Skip to content Skip to sidebar Skip to footer

How Should I Do From Notification Back To Activity Without New Intent

from Android Development, i implement a simple notification by the sample code, but in my app, i don't want to new intent and create the activity again, i just want to back to my l

Solution 1:

If you want to call the Activity from background try this:

    Intent intent = new Intent(this, YourLauncherActivity.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            intent, 0);
    mBuilder.setContentIntent(pendingIntent);

If you click the Notification while on Homescreen the last shown Activity of your App will be get to the foreground without starting it new. If the Activity was killed by the system you will get a new Activity.


Solution 2:

I have used PendingIntent.getActivities instead of getActivity. This is working well in my project.

Intent backIntent = new Intent(this, HomeActivity.class);
                backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent notificationIntent = new Intent(this, NextActivity.class);

                final PendingIntent pendingIntent = PendingIntent.getActivities(this, 1,
                        new Intent[] {backIntent, notificationIntent}, PendingIntent.FLAG_ONE_SHOT);

Solution 3:

For me, this worked :

.setContentIntent(
     PendingIntent.getActivity(
     context,
     0,
     new Intent(context, YOUR_MAIN.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP ),PendingIntent.FLAG_ONE_SHOT))   

FLAG_ONE_SHOT : DO NOT CREATE TWICE (associated with Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)


Solution 4:

In SingleTop mode, I use this method without define explicitly the root activity class:

Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
.setComponent(getPackageManager().getLaunchIntentForPackage(getPackageName()).getComponent());

and this to resume last activity from notification:

builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0));

or this to resume last activity from a service:

try {
    PendingIntent.getActivity(this, 0, intent, 0).send();
} catch (CanceledException e) {
    e.printStackTrace();
}

Post a Comment for "How Should I Do From Notification Back To Activity Without New Intent"