Fragment’s savedInstanceState is always null.

First of all, only fragment killed by system will call onSaveInstanceState() function because of reasons like OOM. Thus, fragment killed by activity’s finish() will not call onSaveInstanceState(). In this situation, it’s better to preserve the state in onPause().

Secondly, in onSaveInstanceState(), one must call super.onSaveInstanceState() at the end of the implemented function.

Thirdly, the fragment should have an TAG or ID associated with it. The best practice is to declare tag in xml or when dynamically adding fragment, use the add() function with tag argument.

Leave a Reply

Your email address will not be published. Required fields are marked *