sharma.mahesh369
sharma.mahesh369

Reputation: 985

Activity stack not getting cleared

I am facing a strange issue

  1. Activity A gets called and Fragment A has been attached to it.
  2. Method gets called (onCreate, onStart,onResume) .

: I received the notification and I tab on that with intent for Activity A.

Blockquote (intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); )

  1. When Activity A is opened it call the below method.

  2. (onPause,onCreate, onStart,onResume (Fragment detached) and than onDestroy)

    Why on destroy is called for the old instance of the activity when I have already called CLEAR_TOP.

Can someone help me what can be the cause for this?

How can I check if there is any instance is pending in stack already or how can I clear everything? I can not use singleInstance as on notification tab I am redirecting to different fragment.

Upvotes: 2

Views: 74

Answers (1)

David Wasser
David Wasser

Reputation: 95578

If you use only FLAG_ACTIVITY_CLEAR_TOP then all instances of activities will be cleared, back to and including the instance of the target Activity, and then a new instance of the target Activity will be created.

If you want to reuse an existing instance, you need to specify both FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP like this:

intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);

This will clear all instances of activities back to (but excluding the target Activity, and call onNewIntent() on the target Activity with the new Intent.

Upvotes: 3

Related Questions