0

What I have:

I have 3 activities in my app with a basic nested structure:

Activity 1 --> Activity 2 --> Activity 3

Activity 2 contains data embedded in an ArrayList<CustomClass>. CustomClass extends application (as taught here) and Activity 3 uses this to modify the CustomClass data for Activity 2.

What I want:

When the user hits the Home button and then opens my App again later, I want to return to whichever activity was on the top of my stack. (I thought this is supposed to be Android default behaviour.)

It works fine on my Eclipse emulator but on both my Google Nexus One and Huawei Honour, the task is completely restarted.

I have tried setting my activities to run in standard, singleTop and singleTask launch modes. The best result I could get on the phones was to return to Activity 1 (which is just a form) and still display what I had entered into the text fields before creating Activity 2.

I did also notice that if I hit Home then check the Running Applications under Settings, my app is not listed. Maybe Android might be destroying my task and I need to save the activity state. But this doesn't make sense if I press home and immediately re-enter the app?

Advice is appreciated.

Community
  • 1
  • 1
ndru1
  • 21
  • 3

1 Answers1

0

There is no guarantee that once you hit the Home button and your Activity goes in onPause() it will keep its state (even it it keeps the same state, there is no guarantee on the actual amount of time the activity will remain in memory). This depends on the device in particular and how it handles its internal memory. (see this figure which shows the Android Activity Lifecycle).

The best practice is to always save your state in the onSaveInstanceState method. This post provides a good and easy example on how to do that.

Community
  • 1
  • 1
Raul Rene
  • 10,014
  • 9
  • 53
  • 75