1

For my first Android app I have (hopefully) followed the tutorial at developer.android.com to the letter. The finished app has an EditText view where you can enter text and a SEND button which will open a new activity (DisplayMessageActivity) which displays the text in the EditText in large font.

I have noticed some inconsistent behaviour regarding the persistence of the text in the EditText. Assume I have entered some text there:

  • When I hit the Back button at the bottom of my phone and restart the app from the icon I loose the text.

  • When I hit the Home buttom at the bottom of my phone and restart the app from the icon I keep the text.

  • When I press the SEND button and leave the DisplayMessageActivity via the Back button at the bottom of my phone I keep the text.

  • When I press the SEND button and leave the DisplayMessageActivity via the arrow button in the action bar I loose the text.

Can anyone explain to me why the behaviour is different and what I have to do to retain the text in all four cases?

Martin Wiebusch
  • 1,681
  • 2
  • 11
  • 15
  • 2
    Because if you press `BACK` then your Activity is killed, while if you press `HOME` it's put in background. If you want to ensure your text is persisted, override `onPause()` method and persist the contents manually. – EpicPandaForce Apr 11 '15 at 14:09
  • Thanks. But what about the difference between the last two bullet points? The first activity should be in the background in both cases, shouldn't it? Also, when I override onPause(), is there some way to exploit the fact that the default implementations of onSaveInstanceState() and onRestoreInstanceState() already do what I want, so that I don't have to do everything manually? – Martin Wiebusch Apr 11 '15 at 14:47
  • 1
    In neither of your examples is any state saved/restored at all. In those cases where state appears to have been saved, you're back to the _same_ instance of the Activity, in the other cases it's a _new_ instance. Best way to learn about this is to override all the life cycle methods and add some debug logging. Then go through all your cases again. – ci_ Apr 11 '15 at 14:47
  • 1
    Back when I made games and wanted to persist the game state, all I did was that I serialized the contents into shared preferences or private files in `onPause` and reloaded in `onCreate`. This way it handled all deaths, putting into background and configuration changes equally. – EpicPandaForce Apr 11 '15 at 14:56
  • @ci_: When I rotate my phone the text in the EditText persists. I've read in several places that a screen rotation causes an activity to be destroyed and then re-created and that onSaveInstanceState() and onRestoreInstanceState() are used to save and restore the state of the activity in that case. So it seems that these methods are already doing what I want. I wonder if there is a way to simply dump the Bundle object into shared preferences. – Martin Wiebusch Apr 11 '15 at 15:56
  • onSaveInstanceState() is not called in all cases, in particular not when the user quits the activity by pressing the back button. http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle) – ci_ Apr 11 '15 at 16:11
  • Shared Preferences stores `primitives and Strings`. – EpicPandaForce Apr 11 '15 at 18:05

0 Answers0