If you need to keep the state of a fragment between its displays, you must override the onSaveInstanceState
fragment onSaveInstanceState
. In your case, it will look something like this:
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putSerializable(FIRST_OBJECT_KEY, firstObject); outState.putSerializable(SECOND_OBJECT_KEY, secondObject); }
The data saved in this way can then be obtained in the onCreate
fragment onCreate
from its savedInstanceState
parameter, like this:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null && savedInstanceState.containsKey(FIRST_OBJECT_KEY)){ firstObject = savedInstanceState.getSerializable(FIRST_OBJECT_KEY); } else { // значит ничего не было сохранено ранее, // то есть видимо фрагмент еще ни разу не создавался } }