Reputation: 407
I'm working on an app and am using the android architecture components. I have a ViewModel that I am using to store and manage the UI data. I have an activity that displays a list of text items that are loaded from the Room database as LiveData. The views containing the LiveData are editable. Basically, it's a RecyclerView containing EditTexts.
What I'm wondering is the scenario where the user changes one of the EditText's values, and then something happens that causes the activity to restart, such as a configuration change. From what I understood, the activity is created again which means the onChanged() callback gets invoked again and the list gets repopulated with the original LiveData that came from the database. Because of that, I expected the EditText (the one whose text was edited by the user) to display the original LiveData that was assigned to it before the user changed the text. However, when I rotated my device's screen, the edited text remained.
This is what I wanted, but I didn't think this was the default behavior. I'd like to know why the edited text persisted after the configuration change since it was never saved to the database. Is this just a special property of EditTexts? This user's post describes something similar happening where their EditTexts retained their most recent states after a screen rotation while their TextViews were cleared: Restoring state of TextView after screen rotation?
Upvotes: 0
Views: 951
Reputation: 407
Found the answer to my question in this codelab: https://codelabs.developers.google.com/codelabs/android-lifecycles/#6
Some UI elements, including EditText, save their state using their own onSaveInstanceState implementation. This state is restored after a process is killed the same way it's restored after a configuration change.
Upvotes: 0
Reputation: 91
I recommend reading this answer. But yes, edittexts will automatically handle state by default. Also, in your scenario the livedata didn't load the value again because the database did not change.
Upvotes: 1