I have a Singleton Data class that I use to store data. I refer to him in different Fragment with.
When the first Fragment loaded, there is no problem that all fields in Singleton are null . When the second Fragment displayed, it depends on these fields to show your data. The first Fragment provides initialization of these fields.
However, when the user clicks the home button in the second Fragment and reopens it after an hour or so, Singleton has lost all his data, and Fragment trying to access the null fields.
I wanted to implement the onSaveInstanceState method, but I do not understand how this works - I do not have a data instance for its purpose.
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putSerializable("DATA", Data.getInstance()); } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); savedInstanceState.getSerializable("DATA");
Any help is appreciated.
source share