8

I want to keep a single instance of every Activity I start in my application. The launchMode singleTask was an option but it is working for only one Activity.

I want

  • to start an Activity if there is no instance and it is called.
  • and if any other instance of that Activity is present already then that instance will brought to front without creating a new instance of that Activity.
  • This property will be applied to more than one Activity.
  • No Activity does guarantee that it will be always on the top of the history stack.

My work until now:

I got many suggestions which are not valid for my case, so I want to point these out so that no other person would give the same suggestion.

  • I have set the launchMode to singleTop and this works only if the Activity is at the top of the history stack. onNewIntent() only gets called if Activity is at the top of history stack. and in my case the Activity may be at any position in stack. So this is not working.
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52

3 Answers3

25

When you launch an Activity, do it like this:

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    @David Wasser: same issue for me. Worked for my problem, cheers! – AJW May 29 '17 at 06:57
  • this will just reorder the Activities - if the activity exists, it will show it (but the old state, so in case you have new data, you will not see them!), and if not exists it will create it – Darksymphony Feb 11 '23 at 14:26
  • @Darksymphony what do you mean "new data"? If the user wants to update the `View`s in this `Activity`, he can do this in `onResume()`, or if he wants to send data to the `Activity` (as "extras" in the `Intent`), the `Activity` can override `onNewIntent()` and process the "extras" there. `onNewIntent()` will be called before `onResume()` when the `Activity` is brought to the front. – David Wasser Feb 12 '23 at 16:47
  • I have a recyclerview with data, from there user clicks edit button - goes to EditActivity. After editing will be redirected back to first activity - no problem yet. Then user clicks edit again, goes to EditActivity, and now he just press the BACk button on his device (not in app) and then once again and the old activity will appear with previous data. As your solution doesn't kill the old activity, just bring it to top. Maybe I was doing something wrong, but I had this experience. For me only thing that helped was: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); – Darksymphony Feb 13 '23 at 18:33
  • 1
    @Darksymphony In this case you would need to refresh the data in `onResume()`. – David Wasser Feb 14 '23 at 06:26
4

You can set the android:launchMode of your activity to singleTop In this case the if the activity already exists, new intents will bring it to front will be delivered to the activity's onNewIntent() http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

This will work if your activity is on the top of the stack.

if you want to have a single instance of the activity, then you can set your launchMode to singleTask, but this is not recommended as it will make your activity reside in a separate task , which can be confusing to the users.

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
0

Use singleTop launch mode instead (docs): if there already is an Activity instance with the same type at the top of stack in the caller Task, there would not be any new Activity created, instead an Intent will be sent to an existed Activity instance through onNewIntent() method.

See can also this article for details on launch modes.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • will give it a try . and let you know. – Sagar Nayak Apr 25 '16 at 12:35
  • so i assume that the activity has to be on the top of history stack to be working in your given condition. and my case is not that . please correct me if i am wrong @Marcin . – Sagar Nayak Apr 25 '16 at 12:51
  • Ok, I got your question slightly different. Actually @david-wasser's answer using `FLAG_ACTIVITY_REORDER_TO_FRONT` is fine, I am upvoting his – Marcin Orlowski Apr 25 '16 at 13:37