申请恢复后没有显示标题的碎片?

时间:2013-06-20 23:02:47

标签: android android-fragments actionbarsherlock android-actionbar

我有一个奇怪的问题,我从来没有见过任何地方,所以我已经在这里发帖,希望我说得够清楚。

我有一个简单的SherlockFragmentActivity如下图所示,其中包含三个片段,这些片段在getActivity().setTitle()中调用onCreateOptionsMenu(),允许我的应用根据哪个片段可见来更改标题。

这可以按照需要运行,但由于某些原因(可能无关)当我通过HOME按钮偶尔退出我的应用程序时,重新打开应用程序时标题不可见。似乎我应该关闭我的应用并重新打开它,它很好但是在我离开它一段时间之后,当我重新打开它时,标题将不会出现。

我完全不知道是什么造成这种情况所以任何帮助都表示赞赏。我的应用程序的布局(与此问题相关)是一个基本的初始屏幕(作为活动),带有一个加载栏,然后打开以下FragmentActivity

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import static java.lang.Math.*;

public class FragmentControl extends SherlockFragmentActivity {

    private static final int NUM_PAGES = 3;

    private ViewPager mPager;

    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_control);

        ActionBar action = getSupportActionBar();
        action.setDisplayShowTitleEnabled(true);
        action.setDisplayShowHomeEnabled(false);

        mPager = (ViewPager)findViewById(R.id.pager);
        mPagerAdapter = new FragmentControlAdapter(getSupportFragmentManager(), NUM_PAGES);
        mPager.setAdapter(mPagerAdapter);
        // If this activity wasn't called after a reload
        if((Integer)getIntent().getExtras().get("current") == null){
            // Always start on the middle page, or as close as possible
            mPager.setCurrentItem((int) ceil(NUM_PAGES/2));
        // Otherwise start on the page we left for a smoother experience
        } else {
            mPager.setCurrentItem((Integer)getIntent().getExtras().get("current"));
        }
    }
}

只有当应用程序重新打开FragmentActivity时才会看到此问题,重新打开其他任何内容并导航到此活动时,这很好(正如您所期望的那样)。

感谢所有的帮助,希望我已经说清楚了。

哦,如果重要的话,我目前的目标是API 17,对API 8的支持最少。我看到这个问题的测试手机是HTC One S - 还不确定其他设备但是我要去开始寻找。

1 个答案:

答案 0 :(得分:3)

  离开它一段时间之后偶尔(...)

这听起来好像你的申请流程在此期间被杀死了。

确保使用onSaveInstanceState保存实例状态(如显示的标题),并在Activity.onCreateFragment.onViewCreated中恢复。

相关问题