Home > Net >  How to exit onBackPressed method in android studio?
How to exit onBackPressed method in android studio?

Time:01-30

I have developed an android apps that have a splash screen and a login page. The problem is when a user give credential and pressed log in button it open controlActivity. but when I pressed back button, I navigate to login page again If I pressed back button again I navigate to splash screen. How can I stop navigating to previous page, when user press back button then app should exit?

CodePudding user response:

You need to put a flag in the activity you want to be forgotten.

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

CodePudding user response:

Why does it happen ?


This is happening because whenever you open another activity from intent, it creates it over it.

How to solve the problem?


  1. You can add a flag like this
Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

OR

  1. You can finish it.
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
  •  Tags:  
  • Related