I am trying to cope with saving state fragmentin order to avoid problems when rotating the screen. This happens strangely: when I rotate the screen for the first time, everything works, but when I rotate the screen for the second time, the application crashes:
Here is the piece of code for the fragment
@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) {
this.nameSurname.setText(getArguments().getString("namesurname"));
this.profileImage.setImageBitmap((Bitmap)getArguments().getParcelable("imgprofile"));
}
}
There is part of the action code here
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "profileFragment", profileFragment);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawner);
if (savedInstanceState != null) {
this.profileFragment = (ProfileFragment) getSupportFragmentManager().getFragment(
savedInstanceState, "profileFragment");
restoredProfile=true;
}
mistake is
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)
Bella source
share