I have fragments for 3 states of a screen; Add, Edit and View. In Add, I create an entity and save it. Next time I open it in View mode and set the entity name using
There are some classes of View in Android should save their status when their container is detached. Fragment.onViewCreated() should be called before View.onSaveInstanceState(). So if you set a value in the method Fragment.onViewCreated(). The value should be cleared in the method View.onRestoreInstanceState(Parcelable state).
For example,class TextView,RecyclerView and so on.You can read the code of TextView.java:
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
// Save state if we are forced to
final boolean freezesText = getFreezesText();
boolean hasSelection = false;
int start = -1;
int end = -1;
....
if (freezesText || hasSelection) {
SavedState ss = new SavedState(superState);
....
}
....
}
There are to params to control whether to save the state: "freezesText" and "hasSelection". TextView can't be selected,so hasSelection is false. the function ,getFreezesText(),return false in class TextView too. So,TextView would not save the state. the code of EditText.java:
@Override
public boolean getFreezesText() {
return true;
}
EditText return true,so EditText should save the state.
There some method to fix this bug:
1.implement EditText.getFreezesText() and return false,and clear the state of select in EditText
2.implement onSaveInstanceState of EditText, return null.like this:
public Parcelable onSaveInstanceState() {
super.onSaveInstanceState();
return null;
}
3.use EditText.setSaveEnable(false);
4.add param in xml " saveEnable='false'"