Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving fragment state after screen rotation

I am trying to deal with saving the fragment's state in order to avoid problem when the screen is rotated. It happens a strange thing: when I rotate the screen for the first time everything works, but when I rotate the screen for the second time application crashes:

Here part of the code of the 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"));
        }
    }

Here there is part of the code of the 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;

    }

the error is this one

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.access$800(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)
like image 202
Bella Avatar asked May 20 '15 01:05

Bella


People also ask

How do I save a fragment state?

Various Android system operations can affect the state of your fragment. To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.

What will happen if an activity with a retained fragment is rotated?

Fragments — Scenario 3: Activity with retained Fragment is rotated. The fragment is not destroyed nor created after the rotation because the same fragment instance is used after the activity is recreated.

What the fragment's method SetRetainInstance Boolean does?

SetRetainInstance(true) allows the fragment sort of survive. Its members will be retained during configuration change like rotation. But it still may be killed when the activity is killed in the background.


2 Answers

canc you check 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();
        }
    }
}
like image 119
QuestionAndroid Avatar answered Sep 21 '22 10:09

QuestionAndroid


Let's see the error, it says your fragment is not in your manager, because when you change the state of screen, your activity will be rebuild, and the fragment is destroyed too. Now, the fragment is not create and not a instance, so your fragment is not in the supportFragmentManager.

For save the state, you can google about this method setRetainInstance(true), and figure out some solutions.

Hope it can help you.

like image 26
Jafir Avatar answered Sep 23 '22 10:09

Jafir