Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with getSupportFragmentManager().putFragment

Tags:

android

I am developing an android application and I want to save my fragment that is in the vector fragments. When I use putFragment in onSaveIstanceState the system throw IllegalStateException

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Salviamo lo stato 


        //salvo all fragment
        for(int i=0;i<fragments.size();i++)
            getSupportFragmentManager().putFragment(outState, Integer.toString(i), fragments.get(i));

    }

This is the logcat :

09-13 10:38:10.230: E/AndroidRuntime(24089): java.lang.IllegalStateException: Fragment GenericPageFragment{417716e0} is not currently in the FragmentManager
09-13 10:38:10.230: E/AndroidRuntime(24089):    at android.support.v4.app.FragmentManagerImpl.putFragment(FragmentManager.java:516)
09-13 10:38:10.230: E/AndroidRuntime(24089):    at it.intects.dashboard.droid.BaseActivity.onSaveInstanceState(BaseActivity.java:180)
09-13 10:38:10.230: E/AndroidRuntime(24089):    at android.app.Activity.performSaveInstanceState(Activity.java:1113)
09-13 10:38:10.230: E/AndroidRuntime(24089):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1188)

Why ? How can I resolve this error ?

like image 327
esoni Avatar asked Feb 02 '26 01:02

esoni


1 Answers

You can't do any fragment transactions after savedInstanceState() was called.

The activity will restore the last attached fragment when the activity returns.

And if you want to do any actions with fragments use:

getSupportFragmentManager().beginTransaction().add(R.layout.container,new MyFragment(), "fragment_tag").commit();

A very good tutorial about fragments

like image 73
meh Avatar answered Feb 04 '26 14:02

meh