如何在旋转期间保存活动中每个片段的私有变量?

时间:2014-07-24 07:30:00

标签: android android-fragments rotation fragment restore

我有一个带有NavigationDrawer的Activity,所以我有几个片段:

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();
        mDrawerMenu = (ListView) findViewById(R.id.drawer_menu);

        mProfileFragment = new ProfileFragment();
        mMainPageFragment = new MainPageFragment();
        mTestPageFragment = new TestPageFragment();
        mSettingsPageFragment = new SettingsPageFragment();
        mOnlineNeededFragment = new OnlineNeededFragment();
        mRegistrationFragment = new RegistrationFragment();
        mAuthorizationPageFragment = new AuthorizationFragment();

        //Adding fragments to NavigationDrawer

    }

    //selecting Fragment from NavigationDrawer
    private void selectItem(int position) {
        Fragment fragment = mDrawerMenuItems.get(position).getPage();

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment)
                .commit();

        //...
    }

    //...
}

每个片段都有自己的私有变量,我用

保存
public class MainPageFragment extends Fragment {

    private int var1;
    private Date var2;
    private Date var3;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            var1 = savedInstanceState.getInt("var1");
            var2 = (Date) savedInstanceState.getSerializable("var2");
            var3 = (Date) savedInstanceState.getSerializable("var3");
        }

        //...
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("var1", var1);
        outState.putSerializable("var2", var2);
        outState.putSerializable("var3", var3);
    }
}

并在“onCreateView”中恢复。但是所有我旋转屏幕活动都会重新创建所有碎片,所以我在所有碎片中丢失了旧状态。

我找到了“getSupportFragmentManager()。putFragment(outState,TAG,Fragment)”。此函数可以将Fragment的实例保存到Bundle并稍后使用“getSupportFragmentManager()。getFragment(savedInstanceState,TAG)”将其恢复,因此Activity不会重新创建此Fragment并且不会丢失私有变量。

但FragmentManager只能保存一个片段,这个片段在此FragmentManager中。当我尝试将所有片段放入Bundle时,我得到例外:“片段ProfileFragment {41a27730}当前不在FragmentManager中。”

如何在Bundle中保存所有碎片以在旋转屏幕后恢复它们?

1 个答案:

答案 0 :(得分:1)

你的问题在于onCreate方法总是创建一个新的片段

    mProfileFragment = new ProfileFragment();
    mMainPageFragment = new MainPageFragment();
    mTestPageFragment = new TestPageFragment();
    mSettingsPageFragment = new SettingsPageFragment();
    mOnlineNeededFragment = new OnlineNeededFragment();
    mRegistrationFragment = new RegistrationFragment();
    mAuthorizationPageFragment = new AuthorizationFragment();

使用if语句检查它是否为空。

if (savedInstanceState == null) {
    mProfileFragment = new ProfileFragment();
    mMainPageFragment = new MainPageFragment();
    mTestPageFragment = new TestPageFragment();
    mSettingsPageFragment = new SettingsPageFragment();
    mOnlineNeededFragment = new OnlineNeededFragment();
    mRegistrationFragment = new RegistrationFragment();
    mAuthorizationPageFragment = new AuthorizationFragment();