简体   繁体   中英

How to finish all activity on click of home button

Suppose there are A, B, C, D and E etc. activities. I'm creating a home button in Activity E. Now if i move from activity A to B, then to C and finally to D.

Now when i click home button, when i am in D activity, i want to finish all my other activity.

So that when the user start the app again it will not open the D activity. Instead it will open from the first activity ie A. So how that can be done. Please suggest me.

Regards Anshuman

In activity D, you just need to define a FLAG_ACTIVITY_CLAR_TOP flag while defining intent to start home Activity A.

For example:

public void btnHomeClick(View v) {
        Intent intent = new Intent(this, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

The best way to finish all activities on press of HomeButton is as described below:

If you need to support only API level 16+ (Android 4.1/JELLY_BEAN and above) finishAffinity(); finishes current activity in which it is invoked as well as all activities immediately below it in the current task that have the same affinity.

To call finishAffinity(); on Home Button press event, you can listen for the Home Button press event as below:

Key code constant: public static final int KEYCODE_HOME; This key is handled by the framework and is never delivered to applications. Hence you need to override below method in your activity.

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

After overriding above method, now you can easily listen HOME Key press in your activity using onKeyDown() method.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
       //The Code Want to Perform. 
    }
});

To finish all activities for pre API level 16 (before Android 4.1/JELLY BEAN), you've got to keep track of all activities in your stack and call finish(); for ALL of them by passing HOME Button pressed event result back to previous calling activities (and no, there is no way to iterate through the Activity stack, so you have to manage all of this yourself using startActivityForResult() and OnActivityResult()).

Do not use System.exit(0); as it does not kill your app if you have more than one activity on the stack. What actually happens is that the process is killed and immediately restarted with one fewer activity on the stack. This is also what happens when your app is killed by the Force Close dialog, or even when you try to kill the process from DDMS. This is a fact that is entirely undocumented, to my knowledge.

You can use System.exit(0); in onclick of your home button.

首先,我们无法覆盖android中的“home”按钮(即硬件按钮)。

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