I read in the Android documentation that by setting my Activity's launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP
flag to my Intent, that calling startActivity(intent)
would reuse a single Activity instance and give me the Intent in the onNewIntent
callback. I did both of these things, and onNewIntent
never fires and onCreate
fires every time. The docs also say that this.getIntent()
returns the intent that was first passed to the Activity when it was first created. In onCreate
I'm calling getIntent
and I'm getting a new one every time (I'm creating the intent object in another activity and adding an extra to it...this extra should be the same every time if it was returning me the same intent object). All this leads me to believe that my activity is not acting like a "single top", and I don't understand why.
To add some background in case I'm simply missing a required step, here's my Activity declaration in the manifest and the code I'm using to launch the activity. The Activity itself doesn't do anything worth mentioning in regards to this:
in AndroidManifest.xml:
<activity android:name=".ArtistActivity" android:label="Artist" android:launchMode="singleTop"> </activity>
in my calling Activity:
Intent i = new Intent(); i.putExtra(EXTRA_KEY_ARTIST, id); i.setClass(this, ArtistActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i);
Single Top If an instance of the activity already exists at the top of the current task in this launch mode, no new instance will be generated, and the Android system will send the intent data through onNewIntent (). If an instance does not exist on top of the task, a new instance will be generated.
singleInstance This is very special launch mode and only used in the applications that has only one activity. It is similar to singleTask except that no other activities will be created in the same task. Any other activity started from here will create in a new task.
The intent-filter inside the activity tells Android which Activity to launch.
Did you check if onDestroy()
was called as well? That's probably why onCreate()
gets invoked every time instead of onNewIntent()
, which would only be called if the activity is already existing.
For example if you leave your activity via the BACK-button it gets destroyed by default. But if you go up higher on the activity stack into other activities and from there call your ArtistActivity.class
again it will skip onCreate()
and go directly to onNewIntent()
, because the activity has already been created and since you defined it as singleTop
Android won't create a new instance of it, but take the one that is already lying around.
What I do to see what's going on I implement dummy functions for all the different states of each activity so I always now what's going on:
@Override public void onDestroy() { Log.i(TAG, "onDestroy()"); super.onDestroy(); }
Same for onRestart()
, onStart()
, onResume()
, onPause()
, onDestroy()
If the above (BACK-button) wasn't your problem, implementing these dummies will at least help you debugging it a bit better.
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