I read that Android automatically saves the content of EditText
objects when an application is about to be stopped or killed. However, in my app the content of an EditText
is lost when screen orientation changes.
Is it normal behaviour? Do I then have to manually save/restore its content with onSaveInstanceState
/onRestoreInstanceState
? Or is there an easier method to tell Android to save it end restore it?
Edit:
I create the EditText
object programmatically, not in XML. This turns out to be related to the problem (see accepted answer below).
The easiest way I found to save an object on onSaveInstanceState is to implement serializable and put in bundle
where myObj class implements serializable and in onCreate() method
One possible cause is that you override onSaveInstanceState but you forget to call the same for super class
super.onSaveInstanceState(outState);
State of all views in activity is auto saved UNLESS you override this functionality. Even if this is obvious, mistakes are possible.
could you use
android:freezesText="true"
in the xml layout?This is not normal behavior.
First and foremost, ensure that you have IDs assigned to your
EditText
controls in the layout XML.Edit 1: It just needs an ID, period. If you're doing this programmatically, it will lose state unless it has an ID.
So using this as a quick & dirty example:
That will solve your problem.
Should that fail, you'll need to save and restore state yourself.
Override
onSaveInstanceState
like so:And then restore in
OnCreate
:Also, please don't use
android:configChanges="orientation"
to try to accomplish this, it's the wrong way to go.