Timothy Rajan
Timothy Rajan

Reputation: 1956

Android - Clicking on Home button to clear activity from stack

In my app, when I click on the home button, I wish to clear the activity stack if the home button is pressed. Could anyone please help me. Lets say there are 2 activities Activity A and Activity B. If we click the home button from Activity B, and relaunch app, I wish to launch the app from Activity A but it is resuming from Activity B. Could anyone please help me resolving this issue.

Upvotes: 0

Views: 593

Answers (3)

noni
noni

Reputation: 2947

Care with the accepted answer, if you rotate your device or another app takes foreground, your View will be destroyed because onPause() method will be called. If you want to destroy your activity by only tapping Home button I recommend this:

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.KEYCODE_HOME){
        finish();            
    } else
        return super.onKeyDown(keyCode, event);
}

Upvotes: 0

Prem
Prem

Reputation: 4831

Try Adding the tag android:clearTaskOnLaunch="true" in your manifest for activity A to have the launcher always go to that activity.

Upvotes: 2

JanBo
JanBo

Reputation: 2943

Just call finish() in your Acitivity B onPause() method.

Upvotes: 2

Related Questions