How to prevent going back to the previous activity?

前端 未结 13 970
眼角桃花
眼角桃花 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.

    0 讨论(0)
  • 2020-11-27 10:03

    This method is working fine

    Intent intent = new Intent(Profile.this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-27 10:04

    finish() gives you method to close current Activity not whole application. And you better don't try to look for methods to kill application. Little advice.

    Have you tried conjunction of Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY? Remember to use this flags in Intent starting activity!

    0 讨论(0)
  • 2020-11-27 10:07

    Just override the onKeyDown method and check if the back button was pressed.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if (keyCode == KeyEvent.KEYCODE_BACK) 
        {
            //Back buttons was pressed, do whatever logic you want
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-11-27 10:09

    I'm not sure exactly what you want, but it sounds like it should be possible, and it also sounds like you're already on the right track.

    Here are a few links that might help:

    Disable back button in android

      MyActivity.java =>
        @Override
        public void onBackPressed() {
    
           return;
        }
    

    How can I disable 'go back' to some activity?

      AndroidManifest.xml =>
    <activity android:name=".SplashActivity" android:noHistory="true"/>
    
    0 讨论(0)
  • 2020-11-27 10:12

    Since there are already many great solutions suggested, ill try to give a more dipictive explanation.

    How to skip going back to the previous activity?

    Remove the previous Activity from Backstack. Simple

    How to remove the previous Activity from Backstack?

    Call finish() method

    The Normal Flow:


    All the activities are stored in a Stack known as Backstack.
    When you start a new Activity(startActivity(...)) then the new Activity is pushed to top of the stack and when you press back button the Activity is popped from the stack.
    One key point to note is that when the back button is pressed then finish(); method is called internally. This is the default behavior of onBackPressed() method.

    So if you want to skip Activity B?

    ie A<--- C

    Just add finish(); method after your startActvity(...) in the Activity B

    Intent i = new Intent(this, C.class);
    startActivity(i);
    finish();
    

    0 讨论(0)
提交回复
热议问题