user1455310
user1455310

Reputation:

Need to handle Back button press

I wanted to keep the activity alive, when the user presses back button of the android device. From the documentation, the back press will call onPause, onStop and finaaly to onDestroy.

Is it possible to keep my activity alive in Stack, when the user presses back button.

Upvotes: 0

Views: 292

Answers (4)

AmniX
AmniX

Reputation: 653

override a method called onBackPresses. and remove or comment super.onBackPressed();

For E.G.-

public void onBackPressed() {

// TODO Auto-generated method stub
//super.onBackPressed();

}

Upvotes: 0

Miki Franko
Miki Franko

Reputation: 687

You should to override the onBackPressed method and do nothing.

@Override
public void onBackPressed() {
}

Upvotes: 1

Lal
Lal

Reputation: 14810

Try this one..Worked for me..

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

        // Back?
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // Back
            moveTaskToBack(true);
            return true;
        }
        else {
            // Return
            return super.onKeyDown(keyCode, event);
        }
    }

OR try this

public void onBackPressed() {
    // TODO Auto-generated method stub
    moveTaskToBack(true);
}

Not sure about the 2nd method as i have not tried it..give it a try..

Upvotes: 2

Anjali
Anjali

Reputation: 2535

Just override below method:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
}

Upvotes: 0

Related Questions