0

Application is having 2 activities. I am using following code in each activity to close the application totally and free all resources when home button is pressed. But when i restart the app, it starts from the activity it left earlier. How to achieve the objective.

@Override
public void onStop(){
    super.onStop();
    super.onDestroy();
}
John Watson
  • 869
  • 3
  • 16
  • 32

4 Answers4

1

You cannot close your application at your need. You can call finish() in every Activity when you navigate away from it. But then again, there is no guarantee that the OS will free up the resources immediately and close your Activity.

Never try to implement something like exiting your application. Read this for a good discussion on it.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
1
  1. Do not close application totally. Let system itself decide.
  2. Do not call super.onDestroy() in onStop method!
  3. Use finish() for activity stoping.
  4. You always have System.exit()
Jin35
  • 8,602
  • 3
  • 32
  • 52
  • 1
    Why don't you like `System.exit`? – Jin35 Jun 20 '12 at 12:32
  • 1
    I had read somewhere that `System.exit()` shouldn't be suggested for Android apps. – Kazekage Gaara Jun 20 '12 at 12:33
  • **WHY** *Do not call super.onDestroy() in onStop method*? – GAMA Jun 20 '12 at 12:39
  • Yea. Found it here : _System.exit() does not kill your app if you have more than one activity on the stack. What actually happens is that the process is killed and immediately restarted with one fewer activity on the stack._ http://stackoverflow.com/a/2632649/828625 – Kazekage Gaara Jun 20 '12 at 12:40
  • @GAMA read about activity lifecycle: http://developer.android.com/reference/android/app/Activity.html – Jin35 Jun 20 '12 at 12:41
  • @Kazekage Gaara Thanks a lot! I have used it in my projects, and it works all right(at least in DDMS I heven't seen my process any more). May be there was some changes? I have testsd on 2.3.3 – Jin35 Jun 20 '12 at 12:47
  • I don't think there would have been any changes regarding it. The Android OS is supposed to handle the closing of applications on it's own, we shouldn't interfere with it. So,even going for `System.exit()` is wrong. – Kazekage Gaara Jun 20 '12 at 12:49
0

Finish all the activity first and before finishing last activity just add the below line

android.os.Process.killProcess(android.os.Process.myPid());

which will kill the process which is started for that application.

Sumant
  • 2,775
  • 2
  • 22
  • 30
  • Read the android documentations. It says : _Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes._ – Kazekage Gaara Jun 20 '12 at 12:30
0

Call finish() instead of this and you must exit the app from the main activity itself.
If you pressing Home button on any activity , it will start the same Activity next time.

Or add more detail in your question for better solution.

GAMA
  • 5,958
  • 14
  • 79
  • 126
AAnkit
  • 27,299
  • 12
  • 60
  • 71