singleTask Launch Mode: A -> B -> C Back Stack
With stack [A, B], launching singleTask C with a different affinity and no existing matching task creates a foreground task [C]; [A, B] remains in the background. Reusing an existing C instead calls onNewIntent() and removes activities above C.
The scenario
Activities A and B form stack [A, B]. B launches C with singleTask. C has a different task affinity, no matching task or C instance exists, and no intent flags override the launch mode.
What happens
C starts as the root of a new foreground task [C]. The original [A, B] task remains in the background. These conditions matter: singleTask does not mean every launch always creates another task.
When c already exists
Android brings the matching task forward, calls onNewIntent() on the existing C, and finishes activities above C. For example, a matching stack [C, D] becomes [C]; C is reused.
WHAT IF THE AFFINITY IS THE SAME? Check the matching task, any existing C instance, and the intent flags. Affinity alone does not establish a result of [A, C], nor does it make B an activity above C when C is absent.
Remember
Separate a fresh launch from reuse. singleInstance additionally keeps its activity alone in its task. Intent flags can change launch behavior.
Interview question
Activities A and B are in a task with stack [A, B]. B launches C, which uses singleTask and has a different task affinity. No task or instance of C exists, and no intent flags override the launch mode. What happens?
- a.One task has stack [A, B] in the background, and a new task has stack [C] in the foreground.Correct
- b.The back stack is [A, C].
- c.The back stack is [C], and the previous activities A and B are destroyed.
- d.The back stack is [A, B, C].
Why? this is the answer
With a different affinity and no existing matching task, C starts as the root of a new task. The original [A, B] task remains in the background. singleTask alone does not specify every possible outcome: existing tasks, affinity and intent flags also matter.
Keep this idea fresh
Try a few questions or explore another bite in Android & Kotlin.
Read the original → developer.android.com
- #android
- #activity
- #launchmode
- #backstack
- #tasks
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles