Search code examples
androidandroid-activitystackdestroy

finish an activity which is not on the top of activity stack


I have three activity A, B and C, C being on top of the activity stack. C contains fragments, f1, f2. On click of a button I want to replace "f1" with "f2" and finish activity A, B. Any thoughts how this can accomplished.


Solution

  • Try to use Intent.FLAG_ACTIVITY_CLEAR_TASK when you start the Activity D. This will clear the all Activity in stack, but including Activity C in your case.

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    

    It will clear all the Activity in stack.

    FLAG_ACTIVITY_CLEAR_TASK flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. i.e., the activity becomes the new root and any old activities are finished. This flag can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK