vendredi 15 mai 2015

Clearing task and navigate to main activity upon clicking notification

I have the following scenario which I am trying to fix:

  1. Application consists of two activities, Activity_A and Activity_B
  2. Currently the user is located in Activity_B
  3. Notification received which leads to Activity_A

HOWEVER When hitting the back key then the navigating out from the App is as follows: Activity_A(from the notification) -> Acivity_B-> Activity_A-> App closes.

What I would like to achieve is that upon clicking the notification, Activity_B will finish and Activity_A from the same Task will be presented to the user.

I went through numerous answers and question which almost describe the same scenario and all answer implementation trials didn't solve my issue.

one thing that gave me what I wanted was if I set in my main activity android:launchMode="singleInstance" but then each activity call seems as if a new app was launched.

Below you can find some code:

final Intent notificationIntent = new Intent(getApplicationContext(), MyDevices.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,0);
final Notification notification = new Notification.Builder(
        getApplicationContext())
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(mNotificationTitle)
        .setOngoing(false)
        .setContentText(mNotificationMessage)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
       .setDefaults(Notification.DEFAULT_ALL).build();

notificationManager.notify(1,notification); 

Aucun commentaire:

Enregistrer un commentaire