0

I have an application with three activities: Login, Register and Main activity.

I want to start the Main activity after a user had logged in or registered. But, when the user presses the 'back' button, he should not see the activity from which he logged in. In other words, I want to clear the activity stack before starting the Main activity.

I saw a few solutions online, for example: this, this and this. They did not help.

This is my code for launching the main activity from the Login activity:

Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);

The code in the register activity is quite similar.

I also tried to put finish(); after I started the activity, but it only closed the current activity. Meaning, if I started in the login activity, moved to the register activity and logged in there (Login -> Register -> Main), When I will press the 'back' button I will be return to the Login activity.

Thank you for your help! Yuval.

4 Answers 4

1

The easy way would be to use "noHistory" attribute in manifest. You can set it for the login activity in your manifest file

<activity
    android:noHistory="true"
/>
0

You can do it like this:

If the user goes to the registration screen then just finish the login activity and when they click the back button on their device you can call the onBackPressed () method. In there you can finish the registration screen and start the login activity. If the user went to the registration screen and then registers, you also want to finish the register activity and open the main menu or something else.

Login:

Click on register? Start register activity and finish the login

startActivity (new Intent (Login.this, Register.class));
finish ();

Register:

Click on submit? Start main activity and finish register activity

startActivity (new Intent (Register.this, Main.class));
finish ();

Click om back button?

@Override
public void onBackPressed (){
startActivity (new Intent (Register.this, Login.class));
finish ();

}

And you can also use in the androidmanifest:

android:noHistory="true"

In the activities you want to have no history (where the user can't go to if they click on the back button). But I prefer to do it in code!

Hope this helps you!

1
  • I thought about doing that, but I hoped that there will be a "cleaner" way. If I won't find anoher way, I will do it that way.
    – Yuval
    Mar 12, 2015 at 7:20
0

Try this:

Intent intent = new Intent(this, SomeOtherClass.class);

// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
2
  • This will not add the SomeOtherClass activity to the stack, but it won't erase the content of the stack.
    – Yuval
    Mar 12, 2015 at 7:18
  • My idea was not to add previous activity to the stack. Mar 12, 2015 at 10:04
0

What you are doing is almost correct, just make a small change in the Intent.

In place of using,

    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    getApplicationContext().startActivity(intent);

use:

    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    getApplicationContext().startActivity(intent);

It will clear the top, start a new task and clear all the previous task as well. Now, all that Android knows is that there exists a MainActivity in your stack. You can write:

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

in MainActivity which will allow the user to exit the application and not go back to the LoginActivity.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.