简体   繁体   中英

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.

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..

You should to override the onBackPressed method and do nothing.

@Override
public void onBackPressed() {
}

Just override below method:

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

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

For EG-

public void onBackPressed() {

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

}

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