34

I have an Activity_1 after a lot of steps, say

Activity_2 > Activity_3 .... in some Activity_n I change some data related to Activity_1 and call it using

Intent intent = new Intent(Activity_n.this, Activity_1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

To refresh the content. But later I can go all the way back to Activity_1 where I started, which has old data.

Instead I want the initial Activity_1' s onResume() to be called, using the above code. Or appropriate Flag

FLAG_ACTIVITY_CLEAR_TOP

consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

That' what the docs say, but not what I am getting.

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

3 Answers3

51

You can add this two lines and try

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Write this in your manifest file inside Activity

<activity
     android:name=".SettingsActivity"
     android:launchMode="singleInstance"
     android:screenOrientation="portrait" >
</activity>

"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.

You can use SingleTask or SingleInstance

"singleTask" - The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

"singleInstance" - Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

Refer this link http://developer.android.com/guide/topics/manifest/activity-element.html

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Nirali
  • 13,571
  • 6
  • 40
  • 53
  • But this is a **Fragment** I am using. Till now I was using `onResume()` of the **Fragment**. Which doesn't contain anything Like `onNewIntent()`.But isn't there a way to clear the whole back stack and call a new **Activity** – Archie.bpgc Mar 12 '13 at 11:02
  • See I have added two lines you can try that way – Nirali Mar 12 '13 at 11:07
  • singleInstance and singleTask 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 recommend to find another solution because these are more for use with widgets etc. – Roel Feb 10 '16 at 15:23
  • `android:launchMode="singleInstance"` was enough for me – NecipAllef Jun 02 '17 at 17:34
21
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

Visit : http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
Nhut Chau Minh
  • 311
  • 2
  • 3
  • 2
    This is a much better version of launchmode="SingleInstance". I've been looking for this Flag. – Community Dec 07 '18 at 04:06
  • This works fine for me. In my case, I need to open an activity from the background and avoid launching the activity twice. I need this behavior only in this one case. – DeniSHow Jul 02 '19 at 08:58
8

Resume Activity from backstack if exists or create a new one if not

android:launchMode="singleTask"

add this line to your app's AndroidManifest.xml and start the activity with a normal Intent.

Ahmed Hashem
  • 145
  • 2
  • 8