Android Retain Callback State After Configuration Change
I understand pretty well about Android lifecycle. I post here because I've observed one weird behavior, anyway this is my own thought. My case is like this: One activity will use a
Solution 1:
Here is why you see this behavior:
- When 'onCreate' method is called first time, it has no saved state. Which means
BundlesavedInstanceStateparameter is null. - When configuration changed, Android paths non-null value in
savedInstanceStateparameter. - After configuration is changed, and
onCreatereturns, Android callsonRestoreInstateState. - By default, all views that have
idare trying to restore their state,EditTextrestores its state too (actually, thatTextViewwho restores most of it). - At some place during state restoration (but after
onCreatemethod is completed) yourEditTextcontrol callssetTexton himself in order to restore text that it had just before configuration changed. - Your new
TextWatcherthat you added inonCreatemethod is notified about this change.
Just to make this clear, your old TextWatcher, that you added on first call to onCreate, is not preserved! Its new TextWatcher, that was added on last call to onCreate, which receives the text change notification.
You can examine TextView.onRestoreInstanceState yourself.
Solution 2:
When ever you rotate the device the onCreate method of activity called again.if you want to prevent to call onCreate when rotating the device do the following code in the Manifest
android:configChanges="orientation|keyboardHidden"and in your activity
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
{
}
else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
{
}
}
Post a Comment for "Android Retain Callback State After Configuration Change"