0

I have a VideoActivity which plays a video, what im trying to implement is when i click home button, i will display a notification, and once i click the notification it will bring the VideoActivity instance back to the front.

here's how i define the Intent for my notification:

Intent notificationIntent = new Intent(context, VideoActivity.class);
        notificationIntent.addFlags( Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

but everytime i click the notification, it turns out that a new VideoActivity will be created.

Qing
  • 1,401
  • 2
  • 21
  • 41
  • Possible duplicate of [Intent - if activity is running, bring it to front, else start a new one (from notification)](http://stackoverflow.com/questions/19039189/intent-if-activity-is-running-bring-it-to-front-else-start-a-new-one-from-n) – GRiMe2D Apr 12 '16 at 09:33
  • @GRiMe2D no, this isn't a duplicate of that one. The linked question is about reordering activities within a task. – David Wasser Apr 12 '16 at 10:59
  • Have a look at this: http://stackoverflow.com/questions/5502427/resume-application-and-stack-from-notification – David Wasser Apr 12 '16 at 11:02
  • Or have a look at this: http://stackoverflow.com/questions/23838983/flag-activity-new-task-not-behaving-as-expected-when-used-in-pendingintent – David Wasser Apr 12 '16 at 11:06

3 Answers3

0

Use this as this will also do the same thing in more elegant manner ( without any side effects )

  final Intent notificationIntent = new Intent(context, YourActivity.class);
  notificationIntent.setAction(Intent.ACTION_MAIN);
  notificationIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
  notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

or

you can use this too

<activity 
  android:name=".YourActivity"
  android:launchMode="singleTask"/>

Description of singleTask

The system creates a new task and instantiates the activity at the root of the
new task. However, if an instance of the activity already exists in a separate
task, the system routes the intent to the existing instance through a call to 
its onNewIntent() method, rather than creating a new instance. Only one 
instance of the activity can exist at a time.
Punit Sharma
  • 2,951
  • 1
  • 20
  • 35
  • No no no! This is not the right way to solve this problem! You do NOT need any special launch mode for this. using special launch modes cause all kinds of other side-effects. This is not a good suggestion. – David Wasser Apr 12 '16 at 10:57
  • @DavidWasser: what will the most suitable answer you suggest ? – Punit Sharma Apr 12 '16 at 11:02
  • You just need to provide a "launcher" Intent for the task in the notification. This will bring the task from the background to the foreground. See http://stackoverflow.com/questions/5502427/resume-application-and-stack-from-notification – David Wasser Apr 12 '16 at 11:04
  • or this http://stackoverflow.com/questions/23838983/flag-activity-new-task-not-behaving-as-expected-when-used-in-pendingintent – David Wasser Apr 12 '16 at 11:06
  • dont you think using launch mode is good rather then creating a activity launcher. :? and the side effect you were talking about is the part of app if you have used singleTop here. Single instance will not give any side effect :) – Punit Sharma Apr 12 '16 at 11:19
  • No. Special launch modes have lots of other side-effects. Especially if the application contains multiple activities. Using special launch modes always creates more problems than it solves. There are very very few situations where you need to use "singleTask" or "singleInstance". Usually only when creating a "launcher" application or home screen replacement. Using a "launch Intent" is just doing exactly the same thing as Android does when you click the application's icon from the HOME screen. It just brings the existing task from the background to the foreground. Which is exactly what you want – David Wasser Apr 12 '16 at 12:03
  • ok got what you are trying to say :) Thanks for that deep explanation !! – Punit Sharma Apr 12 '16 at 12:21
  • You must also set `FLAG_ACTIVITY_NEW_TASK` on the launch Intent. – David Wasser Apr 12 '16 at 12:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108934/discussion-between-punit-sharma-and-david-wasser). – Punit Sharma Apr 12 '16 at 12:37
0

You should try this.

    Intent notificationIntent = new Intent(context, VideoActivity.class);     
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
PRIYA PARASHAR
  • 777
  • 4
  • 15
  • This isn't the right approach either. OP just wants to bring his task from the background to the foreground. There are other ways to do that. See my comments in the question and the linked questions/answers. – David Wasser Apr 12 '16 at 12:32
0

You have to make your activity single task means when it is already running or in background state then its new instance should not be created and already running activity should be brought to front.

For this in your AndroidManifest.xml

where you declared your activity add

android:launchMode="singleTask"

in your activity entry.

  • No no no! This is not the right way to solve this problem! You do NOT need any special launch mode for this. using special launch modes cause all kinds of other side-effects. This is not a good suggestion. – David Wasser Apr 12 '16 at 10:57