i created an App the problem with it is when i exit the app using finish() in OnDestroy() the app still running in the background how to exit that when i exit the App from the menu
-
First of all why are you calling finish() in onDestroy().It is not guaranteed that onDestroy will be called in application LifeCycle it is handled by the JVM.So you should not do any thing in this method.Try using onPause() instead. – Shabbir Panjesha Dec 28 '11 at 06:14
-
is there any static method is running in your application ? – Lucifer Dec 28 '11 at 06:14
-
possible duplication of http://stackoverflow.com/questions/2092951/how-to-close-android-application – kosa Dec 28 '11 at 06:15
5 Answers
try putting a return immediately after the finish() call if you have not done so (it would help if you pasted a snippet of your code).

- 1,685
- 2
- 12
- 8
-
hey thanks i use System.exit(0) at the time of exiting the app now the app wont run in the background. – RizN81 Dec 28 '11 at 07:11
try System.exit(0) to force finish your app

- 3,404
- 6
- 37
- 66
-
You can get some odd behavior using this hence why Google advise against it. Works. Just a cautionary note – Tony Dec 28 '11 at 06:24
-
@Tony yes, it's not a good practice but also, the Android is the one responsible for closing the app. BTW I used this in several real world projects with real devices and never had a problem (maybe I was just lucky ;) – momo Dec 28 '11 at 08:03
-
I did have some strange behaviour when I used it in one of my apps .... having said that it could have been my sh*t code also ;-) – Tony Dec 28 '11 at 08:46
To Kill the application from background also kill using process id. you can use following code
android.os.Process.killProcess(android.os.Process.myPid());

- 53,011
- 55
- 178
- 243
Simply put finish() in your menu item onclick. This will mark the app for finishing. There is no way to exit immediately.
EDIT... and manually stop methods / tasks running. Maybe check if isFinishing is true.

- 2,335
- 21
- 25
Android is quite complex regarding management of app lifecycle. Actually it manages activity lifecycles. You may want to read this - it is REALLY helpful in understanding the most important aspects of it. http://developer.android.com/reference/android/app/Activity.html
But be aware any of your activity classes may exist in memory even AFTER the system has called onDestroy on your activity. Therefore your app might not start the same way as it has done initially. At least the static variables of the activity might still have the same value as when the activity was finished and even destroyed!
All you should do is maximally call finish() and take care of your activity variables, especialy the static ones!
Even calling finish() might not really be necessary, since going back on the last activity does the same.
ps as a small hint, put log into these system methods like finish, onDestroy,... and all the others to better understand what is going on. I am sure you will be suprised :-)

- 10,953
- 12
- 77
- 147