Is there any way to kill the app as soon as it goes to background. For example, if I press the home key and again start the app, it goes to same activity but what I want is that as soon as app goes to background, it should be killed or it should start from the first activity.
Asked
Active
Viewed 230 times
0
-
2http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238 – CommonsWare Jul 24 '12 at 13:51
-
How effective is that, if i get all the activities os my app's process and check if (runningAppProcessInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) and if it return false, i do this : android.os.Process.killProcess(android.os.Process.myPid()); – Rookie Jul 24 '12 at 14:06
-
As the above link states: you can NOT remove your app from the memory unless Android OS claims the resources. However, you can destroy the activities by using `finish` as the below answers suggest. – iTurki Jul 24 '12 at 14:07
-
@Raghav: Do not use `killProcess()` in production code. Do not try to kill your own app. Android will terminate your process when appropriate. – CommonsWare Jul 24 '12 at 14:22
-
but for security reasons, i have to logout the user as soon as the pp goes to background. How else can i do that. – Rookie Jul 24 '12 at 14:25
-
1@Raghav: Use time to determine whether or not the authentication credentials are out of date (e.g., time since logged in, time since last touched the app), not whether the app is "in the background". For example, in `onPause()`, update a `static long lastSeenTime` to be `System.currentTimeMillis()`. If, in `onResume()` of an activity, `lastSeenTime` is too old, `null` out your authentication credentials and redirect the user to your login activity. – CommonsWare Jul 24 '12 at 14:34
-
@CommonsWare Many(but not all) devices allow the user to manipulate the system time which could potentially throw off any authentication checks that are based solely upon it. – FoamyGuy Jul 24 '12 at 14:43
-
2@Tim: Then use `SystemClock.elapsedRealtime()`, which is unaffected by end-user operations other than rebooting. That's probably a better answer here than `System.currentTimeMillis()` -- my apologies for not thinking of that when I wrote my earlier comment. – CommonsWare Jul 24 '12 at 14:48
2 Answers
2
it is not "killing" your app perse, but this will give you the effect you are after.
make the intents used to start your second-Nth activity with the FLAG_NO_HISTORY. That way they will be finished as soon as they leave the screen.
Intent i = new Intent(this, SomeOtherActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
When your activity leaves the screen it will be finished, which will cause it to launch from the starting activity the next time it gets launched from the home screen.

FoamyGuy
- 46,603
- 18
- 125
- 156
-
will it be efficient..I mean if no activity is kept as history, will it effect app – Rookie Jul 24 '12 at 14:09
-
yes, it will effect the app in the exact way that you claim you want it to =). – FoamyGuy Jul 24 '12 at 14:10
-
what if i get all the activities of my app's process and check if (runningAppProcessInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) and if it return false, i do this : android.os.Process.killProcess(android.os.Process.myPid()); – Rookie Jul 24 '12 at 14:11
-
what will happen when i press back button. will it get me to previous screen if i use what u suggested above? – Rookie Jul 25 '12 at 07:50
-2
You can catch the event when the user presses the home button as follows
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
You can also override the onBackPressed()
method in your activity and call finish from there.

Adam Monos
- 4,287
- 1
- 23
- 25
-
I don't think the system will let you listen for the home button in this way. – FoamyGuy Jul 24 '12 at 14:05
-
-
The code snippet catches the home button's press event, the `onBackPressed()` method catches the press event of the back button. – Adam Monos Jul 24 '12 at 14:11
-
@Raghav Note, that this will only finish the activity that you are currently in, not all your activities. – Adam Monos Jul 24 '12 at 14:11
-
2@paradx: "I assure you, this is working" -- I assure you, this does not work on Android 2.1 and higher. I just tested your code and, as expected, the `KEYCODE_HOME` event is not delivered. You are welcome to publish a complete sample project to demonstrate otherwise. – CommonsWare Jul 24 '12 at 14:28
-
@paradx I just tested it, and it is not...The system prevents you from catching the Home button like this because it would be to easily exploitable by malware. – FoamyGuy Jul 24 '12 at 14:30
-
@CommonsWare fwiw I built for 1.6, it is not working at that API lvl either. Though my device is on a later version, perhaps that is why. – FoamyGuy Jul 24 '12 at 14:33
-
I havent tried this in the newer APIs, but on 1.6 devices and before it worked. I assumed it still works. My bad. Sorry for that. – Adam Monos Jul 24 '12 at 14:35