0

I am creating an Activity with android:launchMode="singleTask". In the app, there is another class, not declared in the activity, which keeps itself a static final instance and some journal to show in the activity. When I try to finish the activity by press the back key. The log show the activity is destroyed, as I put

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d("I am destroyed", ".");
}

code in the activity. However, when I start the app again, I find the activity is still the same as I leave it. Then, I press the back key, and remove it from the recent apps list manually. And run the app again. This time, the app shows as it should be.

What's the different between press the back key to finish a app and remove it in recent apps list? And what should I do if I want to finish the app as normal apps do?

Owen Zhao
  • 3,205
  • 1
  • 26
  • 43

2 Answers2

0

Each android application gets it's own process, and in that process, one more more activities can start and finish many times without the process ending.

Static variables don't go out of scope when an Activity ends, but only when the process finally ends.

You could end the entire process by calling System.exit(0) however that sort of thing is usually discouraged and a better solution would be to reinitialize the relevant values when the activity is destroyed.

edit: See this post for more details about closing an Android application:

How to close Android application?

Community
  • 1
  • 1
CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74
  • Thank you. Systme.exit(0) works. Actually in my app, all things need to be done are dealing in a static final instance. And the main activity just handles the UI part. In this way, I can ignore the activity's lifecycle and deal the app just like a desktop app, not an android mobile one. I guess that maybe why System.exit(0) is not couraged. However, as 1 GB RAM is the mainstream in android. Is the current lifecycle still needed? – Owen Zhao Sep 23 '12 at 00:23
0

"Back"ing out of your app does not mean it is finished. Android decides when it's finished, if at all. This is by design, is intended to provide fast launching of apps and works very well. You haven't shown any code so it's only a guess but you are probably doing too much in onCreate() and ignoring onResume(). Take a look at the Android activity lifecycle:

http://developer.android.com/reference/android/app/Activity.html#Lifecycle

Simon
  • 14,407
  • 8
  • 46
  • 61