[android] How to prevent going back to the previous activity?

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 screens, both start a new activity called HomeScreen when successful login/signup occurs. Once HomeScreen is started, I want to prevent the users from being able to return to the login or sign up screens by pressing the BACK key.

I tried using Intent.FLAG_ACTIVITY_NO_HISTORY, but since the application has Facebook integration, when the 'Login with Facebook' is used, Facebook should return to the initial login screen, therefore I should keep a history of these activities.

I thought of overriding the behaviour of the BACK button on HomeScreen to directly finish an application when the button is pressed and I used

@Override
public void onBackPressed() {
    finish();
}

but that also does not work.

This question is related to android back back-stack activity-stack

The answer is


@Override
public void onBackPressed() {
}

When you create onBackPressed() just remove super.onBackPressed();and that should work


There are two solutions for your case, activity A starts activity B, but you do not want to back to activity A in activity B.

1. Removed previous activity A from back stack.

    Intent intent = new Intent(activityA.this, activityB.class);
    startActivity(intent);
    finish(); // Destroy activity A and not exist in Back stack

2. Disabled go back button action in activity B.

There are two ways to prevent go back event as below,

1) Recommend approach

@Override
public void onBackPressed() {
}

2)Override onKeyDown method

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

Hope that it is useful, but still depends on your situations.


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;
}

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.


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:

enter image description here
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();

enter image description here


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"/>

Put finish() just after

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

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!


Put

finish();

immediately after ActivityStart to stop the activity preventing any way of going back to it. Then add

onCreate(){
    getActionBar().setDisplayHomeAsUpEnabled(false);
    ...
}

to the activity you are starting.


If you don't want to go back to all the activities on your application, you can use

android:launchMode="singleTask"

Learn more here: http://developer.android.com/guide/topics/manifest/activity-element.html


paulsm4's answer is the correct one. If in onBackPressed() you just return, it will disable the back button. However, I think a better approach given your use case is to flip the activity logic, i.e. make your home activity the main one, check if the user is signed in there, if not, start the sign in activity. The reason is that if you override the back button in your main activity, most users will be confused when they press back and your app does nothing.


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);

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to back

How to Detect Browser Back Button event - Cross Browser Fragment pressing back button How to go back (ctrl+z) in vi/vim How to prevent going back to the previous activity? Android: Cancel Async Task Back to previous page with header( "Location: " ); in PHP back button callback in navigationController in iOS How do I kill an Activity when the Back button is pressed? Override back button to act like home button

Examples related to back-stack

Android - save/restore fragment state How to resume Fragment from BackStack if exists Android: Remove all the previous activities from the back stack Problems with Android Fragment back stack How can I maintain fragment state when added to the back stack? Fragment onResume() & onPause() is not called on backstack Programmatically go back to the previous fragment in the backstack get the latest fragment in backstack How to prevent going back to the previous activity? Fragments onResume from back stack

Examples related to activity-stack

How to prevent going back to the previous activity?