Store and Retrieve Fragment state data in Android

Save fragment state data in the onSaveInstanceState method. This is called after onPause(), this is where you will store the data that you want to keep when the user come back to this fragment. In this case, the data is a string saved with the name greeting, savedInstanceState.putString("greeting", "Hello");

Retrieve the fragment state data in the onViewStateRestored method. This is called after onActivityCreated(), this is where you will retrieve the data that’s stored from the onSaveInstanceState. The data was a string and saved with the name greeting, so we get it by calling the getString("greeting") on savedInstanceState.

Note: If the onDestroy of the fragment is never called. The Bundle savedInstanceState will be null even if there were data saved in the onSaveInstanceState method. An example of the onDestroy will never called is when you called setRetainInstance(true); in the fragment.