How to prevent going back to the previous activity?

前端 未结 13 1046
眼角桃花
眼角桃花 2020-11-27 09:10

When the BACK button is pressed on the phone, I want to prevent a specific activity from returning to its previous one.

Specifically, I have login and sign up screen

13条回答
  •  半阙折子戏
    2020-11-27 10:00

    Following solution can be pretty useful in the usual login / main activity scenario or implementing a blocking screen.

    To minimize the app rather than going back to previous activity, you can override onBackPressed() like this:

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

    moveTaskToBack(boolean nonRoot) leaves your back stack as it is, just puts your task (all activities) in background. Same as if user pressed Home button.

    Parameter boolean nonRoot - If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.

提交回复
热议问题