2

I launch an activity from a notification I receive. If that notification is pressed it launches an activity. If there are no previous activities on the back-stack, or only a certain one, I want to remove that certain activity and insert my main activity in there and than the new activity.

I found this Thread but I don't understand how he handles it with two intents and flags.

i.e. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK)

Is it wise to do it the way he did it or should I edit the activity stack for that?

I am fairly new to android dev, so some advice could help me here out. Thanks a lot ;)

Update: so I went with the stackbuilder but somehow it doesn't get set right ... I don't find my error, my boolean noActivity gets set for sure, but I think somehow I misunderstood how the stack actually puts a previous activity in there.

private void sendNotification(messageType type, Map<String, String> data, boolean noActivities) {
    Intent i;
    String message = "";

    switch (type) {
        case newFOLLOWER:
            User cur = new User(data.get("other.name"));
            User.lookAT = User.getOtherUserByName(cur);
            i = new Intent(this, other_profile.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            message = data.get("other.name") + " is following you now. Click to see his profile";
            i.putExtra("Notification", data.get("other.name") + " is following you now. Click to see his profile");
            break;

        default:
            i = null;
            break;
    }

    if (i != null) {
        TaskStackBuilder stack = TaskStackBuilder.create(this);

        if(noActivities){
            stack.addParentStack(Photostream.class);
        }

        stack.addNextIntent(i);

        PendingIntent pendingIntent = stack.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("PIC LOC")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentIntent(pendingIntent);


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }

}

Update 2: So after searching quite a lot I found out that I missunderstood how the stack builder works. I found another thread where they described how the adding works. Editing the Manifest in order to have a previous stack. I was to fast and skipped part of the tutorial you provided me so kindly ...

Thanks for your guys help ;)

x3lq
  • 93
  • 1
  • 15

6 Answers6

2

You should use TaskStackBuilder. This is the most efficient way and the TaskStackBuilder structure developed for that reason.

I allways use TaskStackBuilder when Push Notifications are included to my project.

Overriding onBackPress() is not good approach because it requires complex structures in your app and you need to handle many things.

Check this tutorial.

Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
2

You should make stack builder while creating notification like this.

Intent resultIntent = new Intent(this, NotificationTapActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
 // Adds the back stack
stackBuilder.addParentStack(MainActivity.class);
 // Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
PendingIntent.FLAG_UPDATE_CURRENT);

So whenever user will tap on notification, MainActivity will be inserted in stack.

One more solution in which you can handle the back press of NotoificationTapActivity. In which you can check if noting is there in stack then you can finish the current activity and starts MainActivity from there.

Surabhi Singh
  • 803
  • 8
  • 14
1
/**Just use below code inside onMessageReceived()**/

 PendingIntent pendingIntent;
    Uri defaultSoundUri;
    Intent notification_intent;

 String message = "Your Message";

 notification_intent = new Intent(this, YourActivity.class);
 notification_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

 pendingIntent = PendingIntent.getActivity(this, 0 , notification_intent, PendingIntent.FLAG_ONE_SHOT);
        defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(getNotificationIcon(notificationBuilder))
                .setContentTitle(this.getResources().getString(R.string.app_name))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent);

   String[] multilineDescription = new String[]{message};

    message = multilineDescription[0];

    for (int i=1; i<multilineDescription.length; i++)
    {
        message += System.getProperty("line.separator");
        message += multilineDescription[i];
    }

    notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));

    /***Above commented is for strecting the big message in Push Notification******/

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify((int)MESSAGE_NOTIFICATION_ID  /* ID of notification */, notificationBuilder.build());
yash786
  • 1,151
  • 8
  • 18
0

You can simply handle that in onBackPressed of notification activity.

With help of flag you may know that your notification activity is opened through notification click and then in onBackPressed go to your mainactivity as following:

Intent intent =new Intent(context,MainActivity.class) intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);

Shivam
  • 492
  • 5
  • 18
0

PendingIntent can implement multi intent. Just create an array intent

Intent[] intents = new Intent[2];
    Intent intent1 = new Intent(this, MainActivity.class);
    Intent intent2 = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, intents,PendingIntent.FLAG_ONE_SHOT);

And when you finish YouActivity, it will back to MainActivity.

-1
Intent intent = getIntent();                   
String data= intent.getStringExtra("from");                                             
if(data.equalsIgnoreCase("notificaton")                                           
{

 // call Mainactivity

 }                                                                              
 else if(){} 
Bosco Sabin
  • 124
  • 7