Is there some way to "kill" my app when the user goes home, clicking the icon does not bring the app back after pressing home, or turning the screen off. I have to re-run the app in eclipse to get it to come forward.
-
2No, you need to implement onResume() and onRestart() :) Killing your app when it goes out of focus (sight) is not the recommended android way. But yes, you can always call System.exit(0) in your onPause() or onStop() method. – Shark Oct 01 '12 at 16:40
-
I am sorry this question was marked down, I did research. The problem is beyond the original scope. The app can ONLY be opened on my devices by clicking run in Eclipse, the icon does nothing even on a freshly rebooted device. – dmattox10 Oct 03 '12 at 01:41
-
Can you build your app with the android build system and not by Eclipse? – Shark Oct 03 '12 at 10:14
-
I will search for a guide for that today and try it! I have only ever used eclipse. – dmattox10 Oct 03 '12 at 12:50
-
put it in out/target/product/
/external and run mm -B on it... I say this because eclipse will let you run projects that won't normally build under Android; for instance Eclipse will let you run a project whose strings aren't externalized. Android won't build it :) – Shark Oct 03 '12 at 13:32 -
The project builds, and runs, and does everything it should, as it should, if I run it on the device using eclipse. But this is the only way to start and use my otherwise completely working application. – dmattox10 Oct 03 '12 at 17:16
-
Great. try to keep it that way :) now you need to implement the onResume() and onPause() methods to allow you to recover your app. – Shark Oct 03 '12 at 17:20
3 Answers
You shouldn't do that. Instead implement onStart and onResume. Please read this thread for more information.

- 1
- 1

- 64,414
- 37
- 100
- 175
-
Wow, I apologize. I had implemented onStart and onResume. After my laptop crashed and I restored the backup, I did not realize that the backup was in a state previous to when I had implemented them. I did my research, I just didn't read my own code... – dmattox10 Oct 03 '12 at 17:54
Try these two ways.....
- Use System.exit(0)
at the onDestroy()
method of the Activity
.
- You can use finish()
method on the onDestroy()
method of the Activity

- 33,294
- 6
- 48
- 75
-
1Android doesn't guarantee that your onDestroy() will ever be called. If you really want to do it this way, use finish() in onPause(). – Shark Oct 01 '12 at 16:42
-
If the app goes into `Stop` state then, it either goes into `Restart` or to `Destroy` state..... – Kumar Vivek Mitra Oct 01 '12 at 16:50
It's not clear what you're trying to do, or what's going wrong. In general, there should be no problem resuming or restarting your app when the user goes home and then back to your app.
Could it be that your code is crashing when onCreate() is called with a non-null argument?
In general, your code flow should be:
onCreate(Bundle savedstate): If savedstate is null, your activity is starting from scratch. If non-null, it's being restarted after having been killed previously. The savedstate bundle should contain enough information to allow your activity to pick up where it left off.
onStart(): Activity is about to appear on the screen. I tend not to bother implementing this.
onResume(): Activity is about to start accepting input from the user. This is a good time to enable gps, sensors, background threads, or whatever else might be consuming resources. If none of the above applies, then I don't bother implementing this.
onSaveInstanceState(savedstate): Your activity may be going away and it may be killed soon. You MUST save enough information into the savedstate bundle to allow your app to be restarted later.
onPause(): Your activity is about to become inactive. Now is the time to shut down anything you started in onResume(). IMPORTANT: this may be the last call you receive before your app is killed, so now is the time to save any user preferences or other long-term state.
onStop(): Your activity is going off-screen. There is no guarantee that this will actually be called. For this reason, I rarely bother to implement this.
onDestroy(): Your activity is about to go away. Shut down anything you started in onCreate(). There is no guarantee that this will actually be called.

- 9,991
- 11
- 77
- 112