0

My android application generates a notification. This notification is definid in the following way:

NotificationCompat.Builder mBuilder =
   new NotificationCompat.Builder(this)
     .setSmallIcon(R.drawable.ic_action_alarms)
     .setContentTitle("title")
     .setContentText("example text");

Intent resultIntent = new Intent(this, MyActivity.class);

PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(resultPendingIntent);

So when I open the notification drawer and click on the notification area, the actvity MyActivity starts. But, If another instance of the MyActivty is already running, I obtain that two MyActivty instances will running at the same time, while I want that always just one instance at time, runs in my application.

I have tried using

android:launchMode="singleTop"
android:clearTaskOnLaunch="true"

but the result is the same.

How can I achieve this task?

GVillani82
  • 17,196
  • 30
  • 105
  • 172

1 Answers1

1

Ultimately, you probably need to think about using Fragments, instead of Activities for the kind of UI interaction you are wanting (and really the way the NavDrawer is designed to work - and they way the official examples implement it).

You can use different intent flags to bring an old Activity back to the front, but this is really not the intended behavior you ultimately need (which is Fragment related).

Ex Intent Flags

Intent intent = new Intent(this, ActivityExamlpe.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Booger
  • 18,579
  • 7
  • 55
  • 72
  • Ok, you are right, I forgot the FLAG. However, I don't understand why I should use the Fragments. – GVillani82 Jun 09 '14 at 16:00
  • Lots of reasons to use Fragments. Here is a good SO question: http://stackoverflow.com/questions/8597769/when-should-i-use-fragments-in-android-applications-why-to-use-fragments – Booger Jul 15 '14 at 13:38