8

I start from activity A->B->C->D->E ..when i go from D->E there should be no activity in stack but, the user can use back button from D and go to C (without refreshing Activity C, like normal back function)

W00di
  • 954
  • 12
  • 21

3 Answers3

17

You could add a BroadcastReceiver in all activities you want to close (A, B, C, D):

public class MyActivity extends Activity {
    private FinishReceiver finishReceiver;
    private static final String ACTION_FINISH = 
           "com.mypackage.MyActivity.ACTION_FINISH";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        finishReceiver= new FinishReceiver();
        registerReceiver(finishReceiver, new IntentFilter(ACTION_FINISH));
    }

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

        unregisterReceiver(finishReceiver);
    }

    private final class FinishReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ACTION_FINISH)) 
                finish();
        }
    }
}

... and close them by calling ...

sendBroadcast(new Intent(ACTION_FINISH));

... in activity E. Check this nice example too.

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • you r a life saver....thanks a ton... – W00di Aug 14 '13 at 11:10
  • 1
    :) unfortunately there were too many close-voters on this issue, though it is slightly different than the mentioned duplicate. The solution above might appear oversized, however, it is quite flexible and powerful :) – Trinimon Aug 14 '13 at 12:11
  • 1
    Thanks for sharing solution. This case is definitely different, new activity was not in back stack before. – Roman Jan 13 '14 at 20:22
  • This is a fantastic solution. Much more powerful and clear than the others. – MattCochrane Dec 09 '16 at 00:28
5

Add flag FLAG_ACTIVITY_CLEAR_TOP to your intent to clear your other Activities form Back stack when you are starting your E Activity like :

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

then start your Activity :

startActivity(intent)

More Information on : Task and BackStack

Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • 1
    but thats only work if the activity is already in the stack – W00di Aug 14 '13 at 07:16
  • @sabya : pls explain more about your problem , it's a bit unclear – Arash GM Aug 14 '13 at 07:19
  • 1
    from the docs: "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." (see http://developer.android.com/reference/android/content/Intent.html) – Trinimon Aug 14 '13 at 07:20
  • @Arash: I start from activty A and move to Activty B then C,D then I move to Activity E from activity D. But when the user click on back button from E there should be no activity in the stack. – W00di Aug 14 '13 at 07:21
  • @sabya : if your Activity are in the same tasks so this Flag should clear your back stack,just add this flag before you start your last Activity – Arash GM Aug 14 '13 at 07:23
  • @Arash I already did this..its not working.. if i do your method and start activity A instead E it working like u said...but i dont want that. I want a new Activity E to be launched with no stack histroy – W00di Aug 14 '13 at 07:26
0

Add flags to your itent it will clear all activities in a stack

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

this is the right wat to clear back activities already in a stack

Hope this helps..

mananjani
  • 185
  • 5