How to recover Activity, instead of creating new, when clicking the notification?

2

In my app when activity goes to status onPause I trigger its notifications through the NotificationManager. I'd like it to be retrieved into the onResume state if a click was given in the notification. Through this code I can open a new activity, but what I intended was to recover the activity that is in the onPause state:

NotificationManager nm = (NotificationManager)  getSystemService(NOTIFICATION_SERVICE);
PendingIntent p = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MainActivity.class), 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setTicker("Tiker Valor: "+Integer.toString(i));
builder.setContentTitle("Titulo Valor: "+Integer.toString(i));
builder.setContentText("Descrição");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(p);

Notification n = builder.build();
n.flags = Notification.FLAG_AUTO_CANCEL;// Retira a notificação quando esta for clicada
nm.notify(R.drawable.ic_launcher, n);
    
asked by anonymous 04.05.2015 / 14:54

1 answer

2

In AndroidManifest.xml , in the activity declaration, put:

<activity
    android:name="........
    ............
    android:launchMode="singleTop"
    ......
    ......>
</activity>

Or set the flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP in Intent used to launch Activity:

...
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent p = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
...
    
07.06.2015 / 17:13