Naetmul
Naetmul

Reputation: 15552

What happens if startActivity() is called to the already created Activity?

I want to start my MainActivity with a new Intent in my other Activity. The two Activities are in the same app, and the second Activity is actually started from the MainActivity. So the scenario is like this:

  1. MainActivity is created with an Intent
  2. MainActivity starts SecondActivity (but MainActivity is not destroyed yet. It is just stopped)
  3. SecondActivity starts MainActivity with a new Intent (SecondActivity is not closed)

The MainActivity is not flagged. I mean, the Activity's launch mode in the manifest is not set (so, it's default).

I want to know what happens to MainActivity's lifecycle and intent.

Is the Activity re-created? Is onCreate() called? Then is onCreate() called twice, without onDestory()? Or the new MainActivity is newly created and there will be two MainActivities? Will the Intent from getIntent() overwritten?

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

Thanks in advance.

Upvotes: 8

Views: 6421

Answers (2)

sujith
sujith

Reputation: 2421

If you call startActivity() for an Activity with default launch mode(i.e, you didn't mention any launch mode in either in manifest or in Intent) a new instance of the activity is created.

For example, A launched B and again B launched A then Activity stack would be A - B - A. Pressing back key at this point would take you to B then A.

Your can refer to Tasks and BackStack documentation from Android.

Upvotes: 2

Glenn
Glenn

Reputation: 12809

Is the Activity re-created? Is onCreate() called? Then is onCreate() called twice,

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

Will the Intent from getIntent() overwritten?

AFAIK, It's still the same Intent.

Upvotes: 2

Related Questions