屏幕旋转后保存片段状态

Saving fragment state after screen rotation

我正在尝试保存 fragment 的状态以避免屏幕旋转时出现问题。发生了一件奇怪的事情:当我第一次旋转屏幕时一切正常,但是当我第二次旋转屏幕时应用程序崩溃:

这里是片段的部分代码

    //save information: a string and an image
    @Override
    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putString("namesurname", nameSurnameString);
        bundle.putParcelable("imgprofile", bitmap);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null) {
            //Restore the fragment's state
            this.nameSurname.setText(getArguments().getString("namesurname"));
            this.profileImage.setImageBitmap((Bitmap)getArguments().getParcelable("imgprofile"));
        }
    }

这里有activity

的部分代码
//save the fragment
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //THE NEXT ONE IS LINE THAT RAISES THE EXCEPTION  WHEN I ROTATE THE SCREEN 
    //FOR THE SECOND TIME
    getSupportFragmentManager().putFragment(outState, "profileFragment", profileFragment);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    FacebookSdk.sdkInitialize(getApplicationContext());
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawner);

    //getting stuff from intent....

    if (savedInstanceState != null) {
        //Restore the fragment's instance

        this.profileFragment = (ProfileFragment) getSupportFragmentManager().getFragment(
                savedInstanceState, "profileFragment");
        restoredProfile=true;

    }

错误是这个

05-20 03:11:12.423  30088-30088/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Fragment ProfileFragment{425cdd38} is not currently in the FragmentManager
            at android.support.v4.app.FragmentManagerImpl.putFragment(FragmentManager.java:573)
            **at com.mypackage.DrawnerActivity.onSaveInstanceState(DrawnerActivity.java:80)**
            at android.app.Activity.performSaveInstanceState(Activity.java:1185)
            at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1233)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3802)
            at android.app.ActivityThread.access0(ActivityThread.java:158)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5365)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

让我们看看错误,它说你的片段不在你的管理器中,因为当你改变屏幕状态时,你的 activity 将被重建,片段也会被销毁。现在,该片段不是创建的,也不是实例,因此您的片段不在 supportFragmentManager.

保存状态,可以google这个方法setRetainInstance(true),想出一些解决办法

希望对您有所帮助。

你能检查一下吗Why won't Fragment retain state when screen is rotated?

public class SettingsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null)
        {
            // Display the fragment as the main content.
            getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
        }
    }
}