I have an app in which I programmatically create an EditText view. I assign an ID to that view using setId()
myEditText.setId(100);
so that Android automatically saves that object's state when pausing/stopping the app (as I was advised to do here). It works in these cases:
- (1) When I leave the app using the "Home" button: if I then come back to the app, the object's state (displayed text) is restored, as expected.
- (2) On a screen orientation change (which involves Android automatically destroying the activity and restoring it through a
Bundle). The object state is also kept.
However, it doesn't work in this case:
- (3) When I leave the app using the "Back" button: if I then come back to the app, the
EditTextobject is empty.
Any explanation as to why this happens? Does Android really distinguish between leaving the app with "Home" and with "Back"? According to the documentation, the object's state should be automatically preserved, through a Bundle, even when the activity is destroyed. And that clearly happens in case (2). But not in case (3)!
If this is normal behaviour, how could I have the app's state automatically saved and restored when the user presses "Back"? I know I could use the SharedPreferences for that, but I'd rather have Android do that automatically, just as it does in cases (1) and (2).
This happens at least in Android 4.0 and 4.2 (I haven't tested others).