Altaf
Altaf

Reputation: 5170

How to close an application when the Home button is pressed

Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.

Upvotes: 2

Views: 11946

Answers (5)

RiksAndroid
RiksAndroid

Reputation: 825

You could use the launchMode and clearTaskOnLaunch flags on your root activity from your AndroidManifest.xml:

android:launchMode="singleTask"
android:clearTaskOnLaunch="true"

When you again start your app, all activities will be killed.

Upvotes: 3

Vineet Shukla
Vineet Shukla

Reputation: 24021

Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----

  1. The user can go in background if ur app does not provide any way by pressing Back key or Home Key.

  2. You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.

  3. you can even restrict user from pressing any key but u can not control the home key.

Upvotes: 0

user612209
user612209

Reputation:

I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.

There are a couple of ways Im looking to do this: 1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish() 2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()

The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons

The second way sounds messy & difficult to maintain

Id welcome any other ideas Drapes

I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event

Upvotes: 0

J. Taylor
J. Taylor

Reputation: 4855

You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).

What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.

If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).

Anyhow, read through the following answers for more information: Close application and launch home screen on Android

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006529

There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.

Upvotes: 3

Related Questions