7

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

nilesh wani
  • 1,261
  • 5
  • 18
  • 30

11 Answers11

14

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.

nilsi
  • 10,351
  • 10
  • 67
  • 79
Chor Wai Chun
  • 3,226
  • 25
  • 41
  • Intent.FLAG_ACTIVITY_CLEAR_TASK this Field requires API level 11.I'm already used FLAG_ACTIVITY_NEW_TASK.please see above code – nilesh wani May 07 '13 at 06:48
  • New task is not enough clearing the task will leave no histories behind, sorry did not notice it can't be used under API 11. Can't help you here. – Chor Wai Chun May 07 '13 at 06:50
  • @nileshwani After a little search on my previous project, I found that previously, I assign a broadcast listener on every activities I have, so whenever I open up something that needs to be a new task, I send a broadcast, and they will call finish() – Chor Wai Chun May 08 '13 at 01:14
10

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.

Sudhin Philip
  • 644
  • 8
  • 15
  • 1
    Nice solution, just remove this from this.finish(), 'cause it's not referencing activity. – Jumpa Oct 16 '14 at 15:15
4

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

Monish Kamble
  • 1,478
  • 1
  • 15
  • 28
2
Intent intent = new Intent(SourceActivity.this,DestinationActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Use this code before go to login activity or any destination activity.so the all the activity which is in stack that will clear.

Hopefully this will help you.

Prerak
  • 125
  • 1
  • 5
1

finishAffinity() works pretty well for API levels 16+

Blago
  • 4,697
  • 2
  • 34
  • 29
0

Try with FLAG_ACTIVITY_MULTIPLE_TASK

It should works as You have it now, is a Login activity a root(luncher) activity?

You may try in onResume method do smth like this

if(!login) {
    finish();
}

doesn't look nice but should work.

Gustek
  • 3,680
  • 2
  • 22
  • 36
0

you can use onbackpressed in your loginactivity to define what it must do on a backkey press

@Override
public void onBackPressed() {
//what it should do on back
}
Naveen Prince P
  • 4,521
  • 3
  • 16
  • 17
0

In your activity on log out button click

   Button logout = (Button) findViewById(R.id.logout);
   logout.setOnClickListener(new OnClickListener()
   {
    Intent myIntent = new Intent(CurrentActivity.this, LoginActivity.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// clear back stack
    startActivity(myIntent);
    finish();
   });

In LoginActivity click back button. If LoginActivity is the MainActivity on click of button will finish the activity and goto home screen

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • what is problem with above code, it is similar to your code just i wrote onclick event like "Logout" . I can't call finish method bcoz this is not activity class. – nilesh wani May 07 '13 at 07:11
  • when you finish your current activity in popped from back stack and the previous activity in the back stack is displayed. Since the back stack is cleared it will navigate to LoginActivity. – Raghunandan May 07 '13 at 07:13
  • also you the context to call finish() like ((Activity) context).finish(); – Raghunandan May 07 '13 at 07:18
  • when i write ((Activity) context).finish() it encountered exception like ClassCastException – nilesh wani May 07 '13 at 07:50
  • http://stackoverflow.com/questions/10662372/how-to-achieve-something-like-finish-in-a-non-activity-class-in-android – Raghunandan May 07 '13 at 07:57
  • pass context to the constructor of the non activity class use the same to start intent and call finish() – Raghunandan May 07 '13 at 17:24
0

This is work for me , try this one.. and dont forget to unregisterReceiver https://stackoverflow.com/a/3008684/3758898

Community
  • 1
  • 1
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
0

Adding below parameter while launching LoginActivity would help you to solve your problem

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

A - Login Activity

B - Some Activity

C - Some Activity

D - Some Activity - logout from these activity and jump to Login Activity.

Above flag will clear activity stack history from memory.so you are not getting the other activity while pressing back button

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

I hope this will help.

1) I used a Builder to show a message after the user click on the logout button.

2) Once the user click "Take me away!", it will return to the main page and when you press back button on the device it will not show the previous activities.

logout=(Button)findViewById(R.id.btn_logout);

        logout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    final AlertDialog.Builder builder = new AlertDialog.Builder(EngagementActivity.this);
                    builder.setTitle("Info");
                    builder.setMessage("Do you want to logout ??");
                    builder.setPositiveButton("Take me away!", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            Intent intent = new Intent(EngagementActivity.this,MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);

                            finish();

                        }
                    });

                    builder.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            dialogInterface.dismiss();
                        }
                    });

                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            });
xhanix
  • 1
  • 1