Why does FLAG_ACTIVITY_CLEAR_TOP not work?

From the documentation for FLAG_ACTIVITY_CLEAR_TOP:

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.

As you added in your comment, the activity A has been finished before calling B, so this situation doesn’t apply. A new instance of activity A will be launched instead.

As I see it, you have two options here:

1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK flags. This will start activity A as the root of the stack. It works, but any other activities in the stack will be lost. Assuming A was the first activity (or at least, that you are not interested in any previous activities in the task stack) then it doesn’t matter. Note: CLEAR_TASK requires API Level 11.

2) Another possible solution (in case the previous assumption is not true) would be to not use intent flags at all:

  • B starts C with startActivityForResult().
  • Instead of calling A, C finishes, having set a result for B indicating that A must be launched.
  • In B.afterActivityResult(), finish B and launch A.

Leave a Comment