Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Kill all activities on logout

Tags:

android

When a user taps "Logout" within my application, I would like them to be taken to the "Login" activity and kill all other running or paused activities within my application.

My application is using Shared Preferences to bypass the "Login" activity on launch if the user has logged in previously. Therefore, FLAG_ACTIVITY_CLEAR_TOP will not work in this case, because the Login activity will be at the top of the activity stack when the user is taken there.

like image 938
Jason Fingar Avatar asked Sep 04 '12 15:09

Jason Fingar


People also ask

How do I close all activities on Android?

exit(); or finish(); it exit full application or all activity.

How do I stop activity from going back?

In Android we can change the default behaviour of the any activity like keypress, back button press, swap left swap right, to do Disable Back Press we need to override default functionality of onBackPressed() or onKeyDown() method in Activity class.

How do I get Android to automatically logout at a certain time everyday?

You can auto logout your session using the AlarmManager class. Here is the method you should call after the login. Then the BootCompletedIntentReceiver broadcast receiver will be triggered at 19.59. You can write Your action in Broadcast receiver.

How do I go back to previous activity on android?

In the second activity, the back button at the top left can be used to go back to the previous activity.


1 Answers

You could use a BroadcastReceiver to listen for a "kill signal" in your other activities

http://developer.android.com/reference/android/content/BroadcastReceiver.html

In your Activities you register a BroadcastReceiver

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("CLOSE_ALL");
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // close activity
  }
};
registerReceiver(broadcastReceiver, intentFilter);

Then you just send a broadcast from anywhere in your app

Intent intent = new Intent("CLOSE_ALL");
this.sendBroadcast(intent);
like image 170
Karl-Bjørnar Øie Avatar answered Sep 20 '22 23:09

Karl-Bjørnar Øie