I am trying to save the state of two fragments. I want them to redisplay when I change the language of the android device (when I leave the activity and come back). Those two fragments are declared as attributes of the activity and both extends the android.support.v4.app.Fragment class. This is what I do to save them in the activity displaying them:
@Override
protected void onSaveInstanceState(Bundle outState) {
if(settingsFragment.isAdded())
this.getSupportFragmentManager().putFragment(outState,settingsFragment.TAG,settingsFragment);
if(historyFragment.isAdded())
this.getSupportFragmentManager().putFragment(outState,historyFragment.TAG,historyFragment);
}
This is how they are retrieved (keep in mind that this is method is called in the onCreate() method, just after the setContentView() method) :
private void instantiateFragments(Bundle inState) {
FragmentManager manager = getFragmentManager();
if (inState != null) { //the line 254 is just below
settingsFragment = (SettingsFragment) this.getSupportFragmentManager().getFragment(inState, SettingsFragment.TAG);
historyFragment = (HistoryFragment) this.getSupportFragmentManager().getFragment(inState, HistoryFragment.TAG);
} else {
settingsFragment = SettingsFragment.newInstance("Settings","0");
historyFragment = HistoryFragment.newInstance("History","1");
}
}
This is the error that I receive when I'm trying to load back the activity:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.UI.Activities.ExampleActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2802)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2867)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4594)
at android.app.ActivityThread.access$1000(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6134)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:872)
at com.example.UI.Activities.ExampleActivity.instantiateFragments(ExampleActivity.java:254)
at com.example.UI.Activities.ExampleActivity.onCreate(ExampleActivity.java:102)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2867)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4594)
at android.app.ActivityThread.access$1000(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6134)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Can you guys please help me to understand what is going on, and how can I solve this (meaning, how can I save the instance of a fragment from it's activity). Thank you
EDIT For those asking me to show and hide, to display the settingsFragment, this is the method that I use:
protected void displayFragmentSettings() {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
if (settingsFragment.isAdded()) { // if the fragment is already in container
ft.show(settingsFragment);
} else { // fragment needs to be added to frame container
ft.add(R.id.body, settingsFragment, "Settings");
}
// Hide fragment B
if (historyFragment.isAdded()) { ft.hide(historyFragment); }
// Hide fragment C
// if (fragmentC.isAdded()) { ft.hide(fragmentC); }
// Commit changes
ft.commit();
}
I do the same for any other Fragment that I need to display.
The main problem was that I had forgotten to add the super.onSaveInstanceState(...) method:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//this.getSupportFragmentManager().putFragment(outState, myFragment.TAG, myFragment);
}