1

This is my scenario: After visiting several activities, a user logs out from the app and is redirected to the login page. what i want is if user press back button while in login page. i want my application exit/closed, instead back to previous activity

Any ideas on the issue? Thanks.

nathan21
  • 19
  • 1
  • 2

7 Answers7

3

Just write this code in OnClick method of OnClickListener of your logout button.

Intent intent = new Intent(getApplicationContext(),Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

It works for me. Best of luck.

Raman Ghai
  • 186
  • 1
  • 7
1

...or you could check logged state when an activity starts: if user isn't logged, take him to login screen...

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • great idea, strangely haven't thought of that. even though, async tasks could keep running even while you're being logged out. – manmal Sep 24 '11 at 17:03
0

I have used onResume method on activities for validate if user is connected:

@Override
protected void onResume() {
    super.onResume();

    ParseUser currentUser = ParseUser.getCurrentUser();

    if (currentUser == null) {
        finish();
    }
}
porquero
  • 1,159
  • 8
  • 5
0

I implement this behavior in next way. I create LogOut class:

class LogOut {
    void addOnLogoutListener(LogoutListener listener);
    void removeOnLogoutListener(LogoutListener listener);
    void logout();
}

This is a global class. When user want to logout he should call logout() methods - this method notify every listener. In your activity add listener that finishes activity. I always add such listener in BaseActivity class to remove code duplication. Also in App class you can define listener that starts LoginActivity or clear important resources when Logout is happened.

0

Write this on your Login Activity back press method

    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
Brinda Rathod
  • 2,693
  • 1
  • 20
  • 32
0

Override back button to act like home button

and then just call this.finish();

Community
  • 1
  • 1
Thorben
  • 6,871
  • 2
  • 19
  • 19
0

override onBackPressed, in which call android.os.Process.killProcess(android.os.Process.myPid())

West_Link
  • 115
  • 2