20

I want to create notification that when it clicked will bring my app to front but without changing (reload or navigate out) the last activity that was shown.

I tried:

setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT))

But in the new Android 4.3 the application brought to front, but its also start a new instance of MainActivity, and I don't want this.

I want that my app will continue from the last activity that was shown.
How to do that?

nrofis
  • 8,975
  • 14
  • 58
  • 113

3 Answers3

8

You don't ever set Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT. That flag is set by Android when it brings the activity to the front. You setting it has no effect.

There's a few ways to do what you want. Check out this answer

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • @greg7gkb What? The documentation clearly states _"This flag is not normally set by application code, but set for you by the system as described in the launchMode documentation for the singleTask mode."_ Since it is set for you, what is the purpose of you setting it? You can't control or influence any behaviour by setting this flag so why would you set it? – David Wasser Apr 23 '14 at 15:26
  • 1
    I know it is old but nothing worked for me, and i want to share the answer that works - https://stackoverflow.com/a/47329722/2410260 – Raziza O Nov 16 '17 at 12:39
4

Working solution for me was :

    //Resume or restart the app (same as the launcher click)
val resultIntent = Intent(context, MyLauncherActivity::class.java)
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER)
resultIntent.setAction(Intent.ACTION_MAIN)
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentIntent(pendingIntent)  //builder is the notificationBuilder
Tobliug
  • 2,992
  • 30
  • 28
-1

Add this "Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP" instead of "Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT"

Explained here

Hope this helps.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
  • 2
    This won't help and is not the correct use of this flag. `Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP` is only useful if you are starting an activity within your own task. Since the activity being started is in another task (because it is started from the notification bar), this flag has no effect. – David Wasser Oct 14 '13 at 14:46