How does EditText retain its value, but not textview, when the orientation of the phone changes?

What is special about Edittext is that it can save the value, but not Textview and some other widgets, and we should use the onSavedInstance () method for them.

What is the magic feature of EditText that can save values?

If someone can tell how he works domestically.

<---- ---- Update>

How it works internally, point to the piece of code that explains this scenario.

+7
source share
5 answers

What magic is especially important for EditText to preserve value? How it works internally, point to a piece of code that explains this scenario.

This is a selectable property that makes a difference. The following if condition in TextView.onSaveInstanceState will take care of this.

 @Override public Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); // Save state if we are forced to boolean save = mFreezesText; int start = 0; int end = 0; if (mText != null) { start = getSelectionStart(); end = getSelectionEnd(); if (start >= 0 || end >= 0) { // Or save state if there is a selection save = true; } } ... } 

By default, TextView cannot be selected. Therefore, getSelectionStart() and getSelectionEnd() returns -1 , where the save variable executes a false value. To preserve its contents when changing orientation, set the textIsSelectable attribute to true.

Since EditText is selected by default, getSelectionStart() and getSelectionEnd() always return> = 0, where the save variable contains the true value and the contents are saved.

Note. By default, freezesText is disabled. Therefore, the value of mFreezesText is false.

+8
source

Quote from this article: http://tekeye.biz/2012/saving-activity-state

โ€œSwitching the device from portrait to landscape causes Android to stop and restart the action, allowing operations to redraw the screen for different dimensions. When the Activity stops and starts, the usual user experience will be annoying if the input is saved. Android has a couple of methods called onSaveInstanceState (Bundle ) and onRestoreInstanceState (Bundle), which are automatically used to view their data.These methods work only if the data view can be identified, therefore, it is necessary that the EditText has i d. This pair of methods can be overridden in Activity, so state variables that are not associated with input fields can also be saved and restored. "

Remove the id from edittext and try :)

+5
source

By default, an EditText view retains its state โ€” this is done by setting flags in the code that point to the view in order to maintain state when the view is not displayed or focus is lost. Text is automatically saved and restored after the device is rotated. You can disable automatic saving of EditText mode state in the XML layout file by setting the android: saveEnabled property to false:

  android:saveEnabled="false" 

Or programmatically call view.setSaveEnabled(false) .

saveEnabled determines whether this view state will be saved (that is, whether its onSaveInstanceState () method will be called). Note that even if freezing is enabled, the view still needs to have an identifier assigned to it (via setId ()) to save state. This flag can only disable saving this view; all child views can keep their state. the saveEnabled attribute is part of android View - View code . Here are some parts of the code:

 public boolean isSaveEnabled() { return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED; } 

...

  public void setSaveEnabled(boolean enabled) { setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK); } 

...

  void setFlags(int flags, int mask) { int old = mViewFlags; mViewFlags = (mViewFlags & ~mask) | (flags & mask); int changed = mViewFlags ^ old; if (changed == 0) { return; } int privateFlags = mPrivateFlags; /* Check if the FOCUSABLE bit has changed */ if (((changed & FOCUSABLE_MASK) != 0) && ((privateFlags & HAS_BOUNDS) !=0)) { if (((old & FOCUSABLE_MASK) == FOCUSABLE) && ((privateFlags & FOCUSED) != 0)) { /* Give up focus if we are no longer focusable */ clearFocus(); } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE) && ((privateFlags & FOCUSED) == 0)) { /* * Tell the view system that we are now available to take focus * if no one else already has it. */ if (mParent != null) mParent.focusableViewAvailable(this); } } 

....

+3
source

Try this for your text review. I think it can help you.

 <TextView ... android:freezesText="true" /> 
+1
source

Android class is protected by methods

 protected Parcelable onSaveInstanceState () protected void onRestoreInstanceState (Parcelable state) 

therefore, any representation can override these methods and store state status information with them.

TextView extends the View class and implements the protected Parcelable onSaveInstanceState () and the protected void onRestoreInstanceState (Parcelable state) inside it.

consider the implementation of onSaveInstanceState ()

 @Override public Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); // Save state if we are forced to boolean save = mFreezesText; int start = 0; int end = 0; if (mText != null) { start = getSelectionStart(); end = getSelectionEnd(); if (start >= 0 || end >= 0) { // Or save state if there is a selection save = true; } } ... } 
  • as you can see here, the save function depends on one "save" flag, so if the user explicitly sets mFreezesText = true, he will save the text
  • another possibility, if there is a selection cursor, then it will at least return getSelectionStart () = 0, not -1, which forces TextView to do save = true to save the state of the TextView, and what happens with EditText, since EditText has a selection cursor and it extends the TextView.
0
source

All Articles