简体   繁体   中英

How to close an application when the Home button is pressed

嗨,我有超过20个活动的应用程序。我想在按下主页按钮时关闭我的应用程序。

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.

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.

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 (ie 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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM