2

I want to close all activities in th stack before going to new activity.

this is my code

Intent i=new Intent(first.this,secondsct.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        //i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        //i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        //i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(i);
                        finish();

But this is not working. I have tried this earlier but not now. I dont know why. Can anyone know what may be the reason.

Kikki
  • 496
  • 1
  • 10
  • 17

4 Answers4

7

I have tried Intent.FLAG_ACTIVITY_CLEAR_TOP But haven't got proper solution so finally this helps me

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);

This clears all the stack and run only LoginActivity

Bhavin Chauhan
  • 1,950
  • 1
  • 26
  • 47
1

You can try FLAG_ACTIVITY_CLEAR_TOP in conjunction with FLAG_ACTIVITY_NEW_TASK.

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP)

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Filipe Batista
  • 1,862
  • 2
  • 24
  • 39
  • Is there already an instance of the called activity (secondsct) present in the stack ? If you see the documentation about `CLEAR_TOP` it says >"If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent." – Filipe Batista Aug 25 '12 at 23:16
  • No there is no instance of secondsct in the stack. – Kikki Aug 25 '12 at 23:23
  • Theres is a question that has been made with a similiar situation, see it over here http://stackoverflow.com/questions/9940022/clearing-activity-stack-is-not-working – Filipe Batista Aug 25 '12 at 23:34
  • Still not clear why it is not working. I have tried it in 2.1 it worked. now when in 2.2 it's not working – Kikki Aug 26 '12 at 00:26
0

use this function whenever u wanna exit. hope this will solve your problem

 public void exit()
    {
         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
    }
Tushar Narang
  • 1,997
  • 3
  • 21
  • 49
0

I also tried all possible flags and I was invoking the finish() method just as you did. But I found a better way to end up all previous activities by using finishAffinity() method instead.

This solution is proposed here in the third answer. And it solved my problem. Hope it solves yours.

Khalil Hamani
  • 71
  • 2
  • 3
  • 13