How to recover Activity, instead of creating new, when you click on the notification?

Asked

Viewed 221 times

2

In my app when activity goes to the state onPause I trigger her notifications via Notifitionmanager.
I wish she could be restored to the state onResume if a click in the notification.
Through this code I can open a new activity, but what I wanted was to recover the activity that is in the state onPause:

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);

1 answer

2


In the Androidmanifest.xml, in the declaration of activity, place:

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

Or set as flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP in the 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);
...
  • ramaral, it worked. Thank you. Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.