Search code examples
androidandroid-intentandroid-activity

Android "singleTask" activity creates new instance of the Activity


I have two different application called App A and App B.

  • App A : ActivityA with launch mode -> "singleTask"
  • App B : ActivityB with launch mode -> "singleInstance"

Steps:

  1. Open App A which will open ActivityA, ActivityStack : ActivityA
  2. Starting App B with startActivityForResult(), ActivityStack : ActivityA > ActivityB
  3. Open ActivityA via Intent from Activity B , ActivityStack : ActivityA > ActivityB > ActivityA

As you can see the activity stack, It creates new instance of the Activity A instead of returning to the same instance of the ActivityA by calling onNewIntent() method.

I am not sure, is this because of Activity started for result.? Please help to provide proper solution in this scenario. Thank you.


Solution

  • You cannot start a singleTask Activity using startActivityForResult(). If you launch an Activity and expect a result returned, then the target Activity must be launched in the same task. Due to this, Android is ignoring the singleTask launch mode when you call startActivityForResult().