Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 657
Naetmul Avatar asked May 31 '13 03:05

Naetmul


People also ask

What happens when an application calls startActivity for an activity that already has an instance running?

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.

What is the different between setContentView () function and startActivity () function?

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.

How do you call startActivity on Android?

Use this code in your Adapter_Activity and use context. startActivity(intent_Object) and intent_Object. addFlags(Intent. FLAG_ACTIVITY_NEW_TASK);

Can we start activity using application context?

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.


2 Answers

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.

like image 141
Glenn Avatar answered Oct 03 '22 09:10

Glenn


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.

like image 32
sujith Avatar answered Oct 03 '22 10:10

sujith