Weird behavior when startActivityForResult a singleInstance Activity

Viewed 55

I created three Activities in an android app: MainActivity, SecondActivity, ThirdActivity and log the onCreate@taskId event of those Activities.

The launch mode of SecondActivity is set to singleInstance.

Then I put two buttons in MainActivity.

button1 startActivity(SecondActivity).

button2 startActivityForResult(SecondActivity).

button3 startActivity(ThirdActivity).

After click button1 -> click button3 I got the following log, which is as I expected:

MainActivity onCreat@xxx0
SecondActivity onCreate@xxx1 // second activity is expected in a new task
ThirActivity onCreate@xxx0 // third

But, click button2 -> click button3:

MainActivity onCreat@xxx0
SecondActivity onCreate@xxx0 // it seems that startActivityForResult will make singleInstance activity not work
ThirdActivity onCreate@xxx1 // Here is the weird thing, the ThirdActivity is created on a new task!

Can someone explain this?

Here is the repo singleInstanceTest for reproducing.

1 Answers

The documents said

if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.

I using the definition of singleTask launch mode to explain that behavior because it sames

  1. When you click to Button 1 it start SecondActivity and when you get back (I assume you press back or call finish) the instance of SecondActivity pop out BackStack

  2. When you click to Button 2 instance of SecondActivity not exits so System will create a new instance for SecondActivity

To understand about it you should try follow MainActivity -> startActivityForResult(SecondActivity) -> Create button in SecondActivity start ThirdActivity -> in ThirdActivity create button and call startActivityForResult(SecondActivity). Log it to understand.

Related