Android - Force new activities to be launched in same stack

0

What I am trying to do :

I am currently in Activity A (in app A). I launch an Activity B (in app B) using startActivity(). Once I am done with Activity B, instead of just finishing it and expecting Activity A to show up, I actually restore Activity A with an intent in case Activity A was killed in the background.

Current dumpsys logs :
When in Activity A

Stack #0:
Task id #23
userId=0 effectiveUid=u0a123 mCallingUid=0 mUserSetupComplete=true mCallingPackage=null
affinity=com.domain.appA
intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10800100 cmp=com.domain.appA/.ActivityA}
realActivity=com.domain.appA/.ActivityA

When Activity B is launched

Stack #1:
Task id #25
userId=0 effectiveUid=u0a99 mCallingUid=u0a123 mUserSetupComplete=true mCallingPackage=com.domain.appA
affinity=com.domain.appB
intent={act=MY_ACTION flg=0x10000000 cmp=com.domain.appB/com.domain.appB.ActivityB}
realActivity=com.domain.appB/com.domain.appB.ActivityB

Stack #0:
Task id #23
userId=0 effectiveUid=u0a123 mCallingUid=0 mUserSetupComplete=true mCallingPackage=null
affinity=com.domain.appA
intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10800100 cmp=com.domain.appA/.ActivityA}
realActivity=com.domain.appA/.ActivityA

When Activity A is launched from Activity B

Stack #1:
Task id #26
userId=0 effectiveUid=u0a123 mCallingUid=u0a99 mUserSetupComplete=true mCallingPackage=com.domain.appB
affinity=com.domain.appA
intent={act=ACTION_FOR_A flg=0x10820000 cmp=com.domain.appA/.ActivityA}
realActivity=com.domain.appA/.ActivityA

Stack #0:
Task id #23
userId=0 effectiveUid=u0a123 mCallingUid=0 mUserSetupComplete=true mCallingPackage=null
affinity=com.domain.appA
intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10800100 cmp=com.domain.appA/.ActivityA}
realActivity=com.domain.appA/.ActivityA

Erratic behavior observed : Activity A instead of being resumed, is actually being instantiated again resulting in 2 instances and breaking the flow because the 2nd instance is a new one resulting in onCreate() being called whereas the continuity of requires onNewIntent() being called.

NOTE: I am launching the Activity B using flag FLAG_ACTIVITY_NEW_TASK because I want Activity B to be persisted even if Activity A is killed in the background.

Desired behavior :

When sending an intent from Activity B for Activity A, Activity A should resume if it is still alive in the background and should be instantiated in case it has been killed in the background.

Possible solution I am thinking here is maybe there is some flag I can set in the intent while launching Activity B so that there is only Stack #0 and no Stack #1. The second stack being created seems to be issue here. I would be able to better understand if someone can also clarify the difference between task and stack because this does not seem to be consistent with what a task is portrayed to be in Android documentation.

android
android-intent
android-activity
stack
android-task
asked on Stack Overflow Apr 12, 2019 by iammrmehul • edited Apr 12, 2019 by iammrmehul

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0