Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

android - onBackPressed to hide Not destroy activity

i know how to cancel back keypress, so that the activity / main window stays visible:

public void onBackPressed() {
    return;             
}

my aim is to hide the activity, however, without finishing it, how do you do that in the onBackPressed event?

i.e. I would like to get as far as onPause(), but not evoke the onBackPressed() default behaviour that essentially calls finish(). another way to put it is that i would like to mimic onUserLeaveHint() ?

any help appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to mimic the "Home" button on specific Activities just do this:

Below API 5:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Above and on API 5:

@Override
public void onBackPressed() {
  moveTaskToBack(true);
}

It will move the Task to background.. when you return, it will be as it was.

You can see more information about this here: Override back button to act like home button


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...