0

I am making an app where the user can logout by clicking the logout button. Before logging out the user is in the Activty A, then he clicks on the ogout button in the navigation drawer and this takes him to the Activity B( Login screen activity). Now here if I click the back button the Activity A reappears even though I am clearing the activity stack by the following code,

Intent intent = new Intent(curr_context, Activity_B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();

Also in the Activity B (login activity) I have the following code,

public void onBackPressed() {
        if (backPressedToExitOnce) {
            super.onBackPressed();
        } else {
            this.backPressedToExitOnce = true;
            Toast.makeText(curr_context, "Press again to exit", Toast.LENGTH_LONG).show();
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    backPressedToExitOnce = false;
                }
            }, 2000);
        }
    }

I am really clueless as to where am I wrong. Any help would be appreciated. Thanks in advance !!

varunkr
  • 5,364
  • 11
  • 50
  • 99
  • Is there any code in your Activity B that starts activity A if there is a logged in user? or the user shall login every time to that app? – SaNtoRiaN Aug 08 '15 at 21:57

4 Answers4

1

just use the

finish();

i think this will do the trick i had the same problem and tell me if it works

1

When you started Activity A from Activity B did you call "finish()" after the StartActivity ?

Example in ActivityB:

Intent intentActivityA= new Intent(getApplicationContext(),ActivityA.class);    
startActivity(intentActivityA);
finish();

This will terminate ActivityB just after starting Activity A, when you use the back button from ActivityA it should automatically terminate the application as ActivityB is already finished.

Stag
  • 11
  • 2
  • thnx for the reply, yes I used finish() there too !! – varunkr Aug 08 '15 at 22:07
  • Have you tried using "getApplicationContext()" rather than the cached curr_context and does the manifest contain a "parentActivityName" for activityA – Stag Aug 08 '15 at 22:29
1

In Activty_A call finish(); after launching Activity_B
The flags you set in the intent create a new task for B activity, but don't close A for you.

Andrei Tudor Diaconu
  • 2,157
  • 21
  • 26
  • thnx, but I think I am already doing that. Please have a look at the intent portion of the code. – varunkr Aug 08 '15 at 22:06
1

you can add this in the activity A:

// 2.0 and above
@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

with this way, when you click back button it will take you to the launcher instead of the activity B