Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close all activities and exit from an app

My app is intended to allow only logged in user to access the activities. If a user logs out, the Shared preference boolean isLogged in is set to false and the user should not access the rest of activities except the LoginActivity.

However, I am able to access all the previously opened activities by pressing the back button.

I would use finish(); while opening each activity but then I would like users to still use the back button while they're logged in.

I have tried solutions from other similar questions like

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

and on the onCreate() of my LoginActivity I have added

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

When I press the logout option, the previous activity opens instead.

Any suggestions please help me?

like image 200
Mwas Avatar asked Jul 14 '16 12:07

Mwas


People also ask

How do I close all tasks in Android?

Close all apps: Swipe up from the bottom, hold, then let go. Swipe from left to right. On the left, tap Clear all. Close all apps on Android Go: Swipe up from the bottom, hold, and let go.

How do I end an app on Android?

To Close the Application, you can also take "System. exit(0)" 0 is standard or use any exit code.

How do I close down pressed apps?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so.


1 Answers

You should use this flags, that clear your 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);
like image 50
Lucas Ferraz Avatar answered Oct 20 '22 19:10

Lucas Ferraz