Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: perfect way to clear all activity when Logout?

Tags:

android

All activities in my apps having Logout button and user can Logout from any activity. I want to send user to Login Activity without showing previous activity. for this i am using:

Following is the logout method delete session

public void logoutUser() {
    //clearing all data from sharedPreferences
    editor.clear();
    editor.commit();
    Intent intnt = new Intent(contxt, LoginActivity.class);
    // Closing all the Activities
    intnt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    // Add new Flag to start new Activity
    intnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
    // Staring Login Activity
     contxt.startActivity(intnt);

}

from Second activity when user click on logout button then Logout method call. My Second activity class extends SherlockFragmentActivity.

public void Logout(MenuItem v) {
    sessionMngr.logoutUser();
}

I get to the login screen when I press the logout button, but when I press back button on the device it is showing the previous activities - I should go to the Android home screen when I press back button in the login screen.

I've seen some question on stackoverflow but not achieve my goal. somebody said me use android:noHistory="true" in manifest file but what happen when i am in Third activity and press back the button it is showing android home screen but it should go to second activity. I also tried FLAG_ACTIVITY_NO_HISTORY but this does not accomplish my goal either.

I don't Understand where I am wrong. Please does anyone have solution.

Thanks in advance

like image 895
nilesh wani Avatar asked May 07 '13 06:05

nilesh wani


People also ask

How do I close all activities on Android?

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

How do I make Android activity only appear once?

Now in the onResume() method, check if we already have the value of prevStarted in the SharedPreferences. Since it will evaluate to false when the app is launched for the first time, start the MainActivity (Which we want to appear just once) and set the value of prevStarted in the SharedPreferences.


3 Answers

Try specifying both of these:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Also to be safe, call a finish() after starting this activity.

like image 120
Chor Wai Chun Avatar answered Sep 19 '22 20:09

Chor Wai Chun


The best way is to use a LocalBrodCastManager in your application while logout.

When the user press the logout button, you can send a local broadcast using below code.

@Override
public void onclick(View view){

  //do this on logout button click
  final String LOG_OUT = "event_logout";
  Intent intent = new Intent(LOG_OUT);
  //send the broadcast to all activities who are listening
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

Now all the activity should listen to this broadcast. And the activity will look like

@Override 
    protected void onCreate(Bundle savedInstanceState) {

  // Register mMessageReceiver to receive messages.
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter(LOG_OUT));
}

// handler for received Intents for logout event 
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
      //do your code snippet here.
      finish();
  }
};

@Override
protected void onDestroy() {
  // Unregister since the activity is not visible
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}

Note: If you have more than one activity, then it is recommended to extend all the activity from a base activity, and implement this local broadcast manager only in the base activity. So that you need to implement the logout code only in one place.

like image 23
Sudhin Philip Avatar answered Sep 23 '22 20:09

Sudhin Philip


This is how I solved it :

Intent intent = new Intent(context, ActivityLogin.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Works with API >= 11

like image 42
Monish Kamble Avatar answered Sep 21 '22 20:09

Monish Kamble