Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android "single top" launch mode and onNewIntent method

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); 
like image 910
Rich Avatar asked Nov 10 '09 22:11

Rich


People also ask

What is single top launch mode in Android?

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.

What is SingleInstance launch mode?

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.

Where would we specify which activity should launch first in app?

The intent-filter inside the activity tells Android which Activity to launch.


1 Answers

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.

like image 68
znq Avatar answered Oct 05 '22 14:10

znq