What happens if startActivity () is called into an already created activity?

I want to run MainActivity with a new Intent in my other Activity . These two operations are in the same application, and the second action actually starts with MainActivity. So the scenario is as follows:

  • MainActivity is created with the intention
  • MainActivity launches SecondActivity (but MainActivity is not destroyed yet, it just stopped)
  • SecondActivity launches MainActivity with a new Intent (SecondActivity does not close)

The MainActivity function is not marked. I mean, the launch mode of the Activity in the manifest is not set (so, by default).

I want to know what happens to the life cycle and intention of MainActivity.

Is activity restored? onCreate() called? Then called onCreate() twice, without onDestory() ? Or will a new MainActivity be created and there will be two MainActivities? Is the intent from getIntent() ?

I know that Activity.onNewIntent() is called for SingleTop Activities. Then in my situation onNewIntent() not called?

Thanks in advance.

+6
source share
2 answers

Is activity restored? Is onCreate () called? Then onCreate () twice,

Yes, yes and yes, because the default launchMode value is "standard" . Activity with the standard launchMode will create a new instance how many times you want.

Will the Intent of getIntent () be overwritten?

AFAIK, he is the same Intent .

+3
source

If you call startActivity () for an Activity with a default startup mode (i.e. you did not specify any startup mode in the manifest or Intent), a new instance of the action is created.

For example, A starts B and B starts A again, then the Activity stack will be ABA. Pressing the back button at this point will lead you to B, then A.

You can link to Tasks and BackStack on Android.

+3
source

Source: https://habr.com/ru/post/946232/


All Articles