1

I havean Activity_A which has a Log-out option. On clicking that I call the Log-in Activity. So when i Click device back button when i am on Login-in Activity it takes me back to the Activity_A where i have the log-out option. what i wanted is, when i click device back button when i am on log-in Activity it must take me to the device home screen. something like "close application" or "clear activity stack".

I tried this:

Intent intent = new Intent(Activity_A.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

but it didn't work, i also tried using finish() int the above code, then it takes me to the Activity i called before Activity_A (as expected).

I searched for my case, and could not find a perfect solution, can someone please help me achieve what i wanted?

and i have an idea of overriding device back button onClick in my login Activity so that it shows the home screen of the device and clears all the activity stack....is it possible? if yes, is it safe?

This is what i have done in LoginActivity, which works just like i wanted

public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}

But is there any other way to achieve this without overriding onBackPressed()??

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

2 Answers2

2

You need to override the back button event and perform all the desired operation there.

@Override

    public void onBackPressed() {
       //do whatever you desire
       return;
    }
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • call this.finish(); in onBackPressed() to terminate current activity. – Raghav Jun 22 '12 at 08:45
  • Call `finish()` in `Activity_A` as you start your `log-in Activity`. That way, `Activity_A` will be cleared of the stack, and when you press `back ` on `log-in Activity`, you will be taken back to the home screen. – Kazekage Gaara Jun 22 '12 at 08:46
  • yeah, but consider a case where i go A ---> B ----> C ---> D, A is my login Activity and D is my Logout activity, now if i add finish(); in activity D and use device back in Login Activity i will end up in Activity C right??...not the home screen....if i write finish(); in all the activities....i cant go back from D to C, C to B or B to A which is not what i wanted – Archie.bpgc Jun 22 '12 at 08:51
  • Ok. So from D, when you press back, you want to go to home screen, did I get you right? – Kazekage Gaara Jun 22 '12 at 08:54
  • No D is where i have Log-out option, which on click takes me to A....and when i am in A...on device back press, i want to go device homescreen....without overriding onBackPress in A – Archie.bpgc Jun 22 '12 at 08:58
  • Check the Updated part of the Question, the way i override onBackPressed() – Archie.bpgc Jun 22 '12 at 09:01
  • Let me think over it, now that you have specifically mentioned that you don't want to override `onBackPressed()`. – Kazekage Gaara Jun 22 '12 at 09:02
  • not that i don't want to override onBackPress. But is there a better way? moreover i don't have any idea if its safe and recommended??? – Archie.bpgc Jun 22 '12 at 09:12
  • The better way would be to clear the stack using the `Flag` of `Intent`. – Kazekage Gaara Jun 22 '12 at 09:25
  • About it being recommended, you are right, it is not recommended. The back button should do what it is supposed to do. So generally, it isn't recommended to override it. – Kazekage Gaara Jun 22 '12 at 09:25
  • Please try this : `intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);`. It should work. – Kazekage Gaara Jun 22 '12 at 09:31
1

try using startActivityForResult

In Activity_A:

Intent intent = new Intent(Activity_A.this, LoginActivity.class);
startActivityForResult(intent,0);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == 0) {
        this.finish();
    }
}

in LoginActivity:

LoginActivity.this.setResult(0);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213