3
Login > HomePage ->Activity1 ->Activity2->Activity3

If once I have gone to Activity3 then from there to Home page. From there i am trying to logout. It is sending me back to the Login page but if I am pressing the back button of my phone it it showing all the previous activities. Please help me on how can we do this.

This is what i have tried

logout.setOnClickListener(new OnClickListener() {    
    @Override
    public void onClick(View arg0) {
        SharedPreferences myPrefs = getSharedPreferences("SelfTrip", MODE_PRIVATE);
        SharedPreferences.Editor editor = myPrefs.edit();
        editor.clear();
        editor.commit();
        Log.d(TAG, "Now log out and start the activity login");
        Intent loginPageIntent = new Intent(getApplicationContext(), LoginPage.class);
        loginPageIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        loginPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(loginPageIntent);

    }
});
Paritosh
  • 2,097
  • 3
  • 30
  • 42
  • check my answer [here][1] i hope it to be helpfull [1]: http://stackoverflow.com/questions/14001963/finish-all-activities-at-a-time/24833606#24833606 – Dmila Ram Jul 18 '14 at 20:58

4 Answers4

3

Login activity needs to have android:launchMode="singleTop" in Manifest file. Here's the link for stack and back stack.

You also need to remove loginPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); as that will create a new task and put Login as root.

gunar
  • 14,660
  • 7
  • 56
  • 87
  • same issue on device back button cliked all previous are shwoing –  Sep 25 '13 at 11:53
  • Updated the answer. You need to remove `Intent.FLAG_ACTIVITY_NEW_TASK` flag – gunar Sep 25 '13 at 11:56
  • same no effect on device back button pressed all previous are shwoing –  Sep 25 '13 at 11:59
  • and if you replace `getApplicationContext()` with `CurrentActivity.class` - where CurrentActivity is your current activity class? – gunar Sep 25 '13 at 12:02
  • And one extra thing: all your activities MUST be on the same task. How are you starting `ActivityX`? You shouldn't have `Intent.FLAG_ACTIVITY_NEW_TASK` every time you show a new activity as that will create a new task – gunar Sep 25 '13 at 12:04
  • Intent newIntent = new Intent(getApplicationContext(), ActivityNew.class); startActivity(newIntent); this how i am staring every activity –  Sep 25 '13 at 12:05
  • How can you be sure the activities are all in the same task? replace `getApplicationContext()` with current activity instances ... as there are other contexts each time – gunar Sep 25 '13 at 12:07
  • same issue no effect i have done the changes as u suggested me –  Sep 25 '13 at 12:13
  • Are you finishing the current activity once you start a new one? – gunar Sep 25 '13 at 12:16
3
// check sharedPreferences in all activities
// when you press back button then it will close activity
@Override
protected void onResume() {
    super.onResume();
    int userId = sharedPreferences.getInt(Login.user_id, 0);

    if(userId==0){
        finish();
    }
}
Paritosh
  • 2,097
  • 3
  • 30
  • 42
Abhijit S
  • 181
  • 1
  • 7
1

you can simply force the activity to NOT leave a history in your backstack. This can cause trouble, but should work fine as long as your activities are called linear.

Add the line with noHistory to the manifest:

<activity
        android:name="com.example.MainActivity"
        android:label="@string/app_name"           
        android:screenOrientation="portrait"

        android:noHistory="true" >
</activity>
bofredo
  • 2,348
  • 6
  • 32
  • 51
0

You can use another way i.e adding activity in an arraylist of type activity and finish the activity you want from any other activity.

droid
  • 414
  • 2
  • 7
  • 21