How to close all actions and exit the application

My application is designed to access only registered users. If the user logs out, the boolean isLogged in general privilege is set to false, and the user should not use other actions than LoginActivity.

However, I can access all previously opened actions by clicking the "Back" button.

I would use finish(); by opening each action, but then I would like users to still use the back button when they are logged in.

I tried solving other similar issues like

 Intent intent = new Intent(getApplicationContext(), LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); 

and on onCreate() my login function I added

 if (getIntent().getBooleanExtra("EXIT", false)) { finish(); } 

When I press the exit button, the previous action opens instead.

Any suggestions please help me?

+5
source share
5 answers

You must use these flags to clear TASK and create a new one.

 Intent intent = new Intent(getApplicationContext(), LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); 
+4
source

Try

You need to add this flag to Intent ..

 Intent intent = new Intent(getApplicationContext(), LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("EXIT", true); startActivity(intent); finish(); 
+4
source
 if (getIntent().getBooleanExtra("EXIT", false)) { finish(); android.os.Process.killProcess(android.os.Process.myPid()); } 
+3
source

Try adding all the activity to the array, and when you want to delete, just remove it from the array and complete this action. see my answer here Remove activity from stack

+2
source

I faced a similar situation. As I understand it, even if I call the finish() function at the beginning, the entire onCreate() function is executed. Then he will finish. In my script, in the onCreate() function, I call another action. That way, even if the main action completes after onCreate() , the second action still exists. So my decision was,

private boolean finish = false;

in onCreate () of the main function,

 if(intent.getBooleanExtra("EXIT", false)) { finish(); finish = true; } 

and I checked all the navigation files

 if(!finish) { Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivity(intent); finish(); } 

My exit function was

 Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); 
0
source

All Articles