New Intent() Starts New Instance with Android: Launchmode="Singletop"

New Intent() starts new instance with Android: launchMode= singleTop

This should do the trick.

<activity ... android:launchMode="singleTop" />

When you create an intent to start the app use:

Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

This is that should be needed.

Setting launchMode= singleTask vs setting activity launchMode= singleTop

I think your definition of singleTop and singleTask is a little off. SingleTop could produce a duplicate instance. Lets use your example, League > Team > Position > Player. If there is a button in the player screen that will take you to the league screen, it will become League > Team > Position > Player > League.

Whereas singleTask guarantees that only one instance of the activity can exist.

Why Android OS does not create a new instance of an singleTop activity if my app in the background?

This behaviour actually has nothing to do with the launch mode of the Activity. Actually, in this case, Android isn't launching any Activity. You would see this if you added logging or set a breakpoint at onNewIntent() which would be called in the case where Android wanted to launch the Activity, saw that there was already an instance on top of the stack in the task, and routed the new Intent to the current instance by calling onNewIntent().

What is haooening here, when the user taps the app icon on the HOME screen, is that, before launching any Activity, Android looks to see if there is already a task in the background that was started with this same Intent (in this case, ACTION=MAIN, CATEGORY=LAUNCHER, COMPONENT=ActivityA). If it finds a task that was started with the same Intent, it simply brings that task to the foreground in whatever state it was in (exactly as it was when it was moved to the background) and that's it. It doesn't launch any new Activity, it does not call onNewIntent() on the topmost Activity.

This is documented here (although it is easy to miss): where it says:

The device Home screen is the starting place for most tasks. When the
user touches an icon in the app launcher (or a shortcut on the Home
screen), that app's task comes to the foreground. If no task exists
for the app (the app has not been used recently), then a new task is
created and the "main" activity for that app opens as the root
activity in the stack.

Android singleTask or singleInstance launch mode?

From the Application Fundamentals page of the Android dev guide:

By default, all the activities in an
application have an affinity for each
other — that is, there's a preference
for them all to belong to the same
task.

A "singleInstance" activity stands
alone as the only activity in its
task. If it starts another activity,
that activity will be launched into a
different task regardless of its
launch mode — as if
FLAG_ACTIVITY_NEW_TASK was in the
intent. In all other respects, the
"singleInstance" mode is identical to
"singleTask".

As noted above, there's never more
than one instance of a "singleTask" or
"singleInstance" activity, so that
instance is expected to handle all new
intents. A "singleInstance" activity
is always at the top of the stack
(since it is the only activity in the
task), so it is always in position to
handle the intent. However, a
"singleTask" activity may or may not
have other activities above it in the
stack. If it does, it is not in
position to handle the intent, and the
intent is dropped. (Even though the
intent is dropped, its arrival would
have caused the task to come to the
foreground, where it would remain.)

4 Activities in a Task

Since there is never more than one instance of the Activity with either launch mode, the back button will always take you to the existing instance of the Activity in your case.

An important difference is that "singleTask" doesn't require the creation of a new task for the new Activities being launched when something is selected. Nor will it have to remove that new task on the back button each time.

Since your Activity stack does all pertain to one user "task", and it doesn't sound like you have an intricate Intent structure where singleInstance may be beneficial to always handle them, I would suggest using the singleTask launch mode.

Here is a good blog post for more info, as well as credited for the image: Android Activities and Tasks series – An introduction to Android’s UI component model

Android single top launch mode and onNewIntent method

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.

Android singleTask activity creates new instance of the Activity

You cannot start a singleTask Activity using startActivityForResult(). If you launch an Activity and expect a result returned, then the target Activity must be launched in the same task. Due to this, Android is ignoring the singleTask launch mode when you call startActivityForResult().

About android launchmode singleTask

The ActivityC will be removed from task1 and ActivityB becomes the top Activity.

Yes, you are Right...

ActivityC will be removed from i.e. the onDestroy method of the ActivityC will be called. Hence when the user launches Task 1 again, the ActivityB is shown rather than ActivityC.

Have created 2 Tasks (Projects) and uploaded the same @ SendSpace. Try it out...

If you look at androids documentation it says

" A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task."

This means that when you click the home button all the activities above the single-task activity (which in your case is ActivityB) are removed from the stack.

In the sample, the app's I had given you earlier if you just run the project "AndroidTest" and click the home button in the logs you can see that the 2nd Activity is put on Pause, and when you launch it again from the "Recent App's" list the 2nd Activity is Destroyed.

In a scenario where the Activity's above the Single Instance activities (ActivityB) are not removed from the Back Stack, and another application request this Activity (ActivityB) it may not be shown and the intent may be dropped. But this has extremely fewer chances of happening because the user will have to press the Home button and but the current Task\App in the BackStack before he could navigate to another Task\App.

Hence the warning

The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.

I hope this solves your doubts.



Related Topics



Leave a reply



Submit