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 ?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With