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:
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.
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.
So, with respect to time, setContentView alone is faster, since it doesn't start a new activity. Hence, your app will show the new screen faster... On the other hand, if you call startActivity, this activity is put on the stack, so you can go back by pressing the back button.
Use this code in your Adapter_Activity and use context. startActivity(intent_Object) and intent_Object. addFlags(Intent. FLAG_ACTIVITY_NEW_TASK);
In order to call startActivity with application context, include the flag FLAG_ACTIVITY_NEW_TASK. Also think about changing the name from context to appContext so it is clear which context you expect.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With