42

I'm finding that in my application, the user can get quite 'nested' in the various activities that are opened while the user is using the application.

For example:

  1. Main Menu
  2. Object List
  3. Object Details
  4. Object Edit
  5. Object Details
  6. Object Child Details
  7. Object Child Edit
  8. Object Child Details

Now, when the user presses back, it has to go through 'Object Child Details' twice (same object, when it is edited it returns to the detailed page), and the same thing happens for the 'Parent Object Details'.

Is there a way to reuse activities, if they are already open in the stack, and reorder them to the front? The only way I have seen is on activities with the launcher attribute. I believe I saw singleTask and singleTop.

If am supposed to be using these two attributes, singleTask and singleTop, how am I supposed to use them? When I tried to include them in the application, it made no difference. Do I also need to set a flag while launching the intent using startActivity?

Cody
  • 8,686
  • 18
  • 71
  • 126

10 Answers10

55

in Manifest Activity property you can give this parameter android:launchMode="singleInstance"

Read in more detail here http://developer.android.com/guide/topics/manifest/activity-element.html

clever_trevor
  • 1,530
  • 2
  • 22
  • 42
ud_an
  • 4,939
  • 4
  • 27
  • 43
  • 20
    **Do not use this without understanding all the implications of `launchMode="singleInstance"`. This does _not_ actually _re-order_ activities**, because it forces the activity to be in a separate task altogether (i.e., it will appear separate from the app in the Recent Apps list). So if you do this for every activity in your app, you'll see each of them as separate entries in the Recent Apps list. At least that's what the docs say. The [reality is a little different](https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en). – Vicky Chijwani Dec 27 '16 at 06:49
  • 1
    My recommendation is to use `Intent.FLAG_ACTIVITY_REORDER_TO_FRONT` instead (as suggested in Vikram's answer below), although it's not a perfect solution. – Vicky Chijwani Dec 27 '16 at 06:55
41

This is your flag! http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)

Note the 'addFlags'. Also note that, onCreate will not be called on this Activity when a new Intent is delivered to it. It will be delivered via the onNewIntent().

This does not ensure that there is a single instance of the Activity running. To ensure that, check this out: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Vikram Bodicherla
  • 7,133
  • 4
  • 28
  • 34
34

Using android:launchMode="singleTask" is probably the best approach, since it won't recreate the activity if it's already running. Just add it to the activity in your AndroidManifest.xml, and you should be all set.

<activity
    android:name=".MyActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
 </activity>

Here's another question that might be useful: Android singleTask or singleInstance launch mode?

Community
  • 1
  • 1
Balu
  • 938
  • 8
  • 10
10

This worked for me.

Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

If an instance of this Activity already exists, then it will be moved to the front. If an instance does NOT exist, a new instance will be created.

A J
  • 4,542
  • 5
  • 50
  • 80
7

Yes you can demand only one instance of these activities be created, but it is generally not recommended. If you simply are concerned about history, take a look at Intent.FLAG_ACTIVITY_CLEAR_TOP.

kabuko
  • 36,028
  • 10
  • 80
  • 93
3

Add intent Flags as

Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP); StartActivity(srcActivity.java, DesiredActivity.class);

Then at onPause() DesiredActivity

Add finish(), This did the work for me.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
P-RAD
  • 1,293
  • 2
  • 15
  • 36
1

in kotlin:

fun AppCompatActivity.startSingleActivity(intent: Intent) {
    startActivity(intent.apply {
        flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
    })
}
LLL
  • 101
  • 4
0

I wanted to have only one Instance of MainActivity, but when I add:

launchMode="singleInstance"

to my manifest a white screen appears before all of my app activities, so I removed singleInstance code from manifest. I added two public static variable of my MainActivity to MainActivity Class:

public static MainActivity mainActivity_instance = null;
public static MainActivity mainActivity_OldInstance = null;

then I added the following code to onCreate():

 if(mainActivity_instance != null)//if true, it means there was an instance of mainActivity before and it had to be closed.
        mainActivity_OldInstance = mainActivity_instance;
    mainActivity_instance = this;
 if(mainActivity_OldInstance != null)
        mainActivity_OldInstance.finish();

As you see, I finished previous instance of MainActivity just as the new Instance created!

farhad.kargaran
  • 2,233
  • 1
  • 24
  • 30
0

https://developer.android.com/guide/topics/manifest/activity-element

android:launchMode = "singleTop"

If the activity is on top of the stack, new instance of activity is not created otherwise new instance of activity will be created. This will preserve the normal flow of activities which is expected by general user base.

-1
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);

It works for me :)

शु-Bham
  • 952
  • 1
  • 7
  • 14