如何在旋转后刷新片段?

时间:2014-02-12 14:25:50

标签: android android-fragments

我有一个活动,其中有两个片段添加到tabAction。第一个是应用程序的gridview,第二个是文档的gridview。当我旋转平板电脑时,我的片段不是最新的。我希望在旋转后实例化我的片段,以便Activity重新启动;但问题是我不使用viewpager。并且下面给出了部分代码:

  // ActionBar
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayUseLogoEnabled(false);
actionbar.setDisplayShowTitleEnabled(false);
// create new tabs and set up the titles of the tabs
ActionBar.Tab mFindTab = actionbar.newTab().setText(
        getString(R.string.ui_tabname_find));
ActionBar.Tab mChatTab = actionbar.newTab().setText(
        getString(R.string.ui_tabname_chat));
ActionBar.Tab mMeetTab = actionbar.newTab().setText(
        getString(R.string.ui_tabname_meet));
ActionBar.Tab mPartyTab = actionbar.newTab().setText(
        getString(R.string.ui_tabname_find));

// create the fragments
Fragment mMeetFragment = new ApplicatinFragment();
Fragment mFindFragment = new MatFragment();
Fragment mChatFragment = new DocumentFragment1();

Fragment mPartyFragment = new PartyFragment();

// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,
        this.getBaseContext()));
mChatTab.setTabListener(new MyTabsListener(mChatFragment,
        getApplicationContext()));
mMeetTab.setTabListener(new MyTabsListener(mMeetFragment,
        getApplicationContext()));
actionbar.addTab(mMeetTab);
actionbar.addTab(mFindTab);

我的onSaveInstanceState由以下代码给出:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    /*
    Toast.makeText(
            this,
            "onSaveInstanceState: tab is"
                    + getActionBar().getSelectedNavigationIndex(),
            Toast.LENGTH_SHORT).show();   */
    outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
    outState.putStringArrayList("List", LaunchActivity.myList);
    outState.putString("ListActivity", LaunchActivity.myList.toString());

}

然后在我的Oncreat中我打电话:

if (savedInstanceState != null) {
            savedUser = savedInstanceState.getString("TEXT");
            savedMode = savedInstanceState.getString("MODE");
            String str;
            str = savedInstanceState.getString("ListActivity");
            test = savedInstanceState.getStringArrayList("List");
            //actionbar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 1));
            // myList = (ArrayList<String>) Arrays.asList(str.split("\\s*,\\s*"));
            actionbar.setSelectedNavigationItem(savedInstanceState.getInt(
                    TAB_KEY_INDEX, 0));
        } else {
            savedUser = "eleve";
            savedMode = "normal";

        }

我想使用 what's the right way to store fragment's state

中给出的解决方案

ViewPager和片段 - 什么是存储片段状态的正确方法] 1

0 个答案:

没有答案