I'm trying to save data in a Fragment's onSaveInstanceState(), but the method is never called.
Can someone help?
public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ScrollView content = (ScrollView) inflater.inflate(R.layout.content, container, false); // More stuff return content; } @Override public void onSaveInstanceState(Bundle icicle) { // NEVER CALLED super.onSaveInstanceState(icicle); //More stuff } }
java. In that fragment I use onSaveInstanceState and onCreate method.In onCreate method I supposed to clear sharedPreferences if there is no bundle passed from onSaveInstanceState, if onSaveInstanceState pass a bundle I would not need to clear sharedPreferences.
Basically, onSaveInstanceState is used for the scenario where Android kills off your activity to reclaim memory. In that scenario, the OS will keep a record of your activity's presence, in case the user returns to it, and it will then pass the Bundle from onSaveInstanceState to your onCreate method.
onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity. onCreate() .
Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.
I finally figured out the problem, at least in my case. I had an overridden onSaveInstanceState
in my FragmentActivity that did not call super.onSaveInstanceState(Bundle outState)
. Once I added that in, the Fragment.onSaveInstanceState(Bundle outState)
functioned normally.
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