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.

4

1 回答 1

0

无法保证一旦您点击 Home 按钮并且您的 Activity 进入,onPause()它将保持其状态(即使它保持相同的状态,也无法保证 Activity 将保留在内存中的实际时间量)。这尤其取决于设备以及它如何处理其内部存储器。(参见此图,它显示了 Android 活动生命周期)。

最佳做法是始终将您的状态保存在onSaveInstanceState方法中。这篇文章提供了一个很好且简单的例子来说明如何做到这一点。

于 2012-07-04T17:15:36.687 回答