[ACCEPTED]-Resume application and stack from notification-android

Accepted answer
Score: 170

Just use the same intent filters as Android 5 uses when it launches the app:

final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

As the Intent you 4 created to open your Activity from the notification 3 bar is the same as Android used for launching 2 your app, the previously opened Activity will be 1 shown instead of creating a new one.

Score: 45

For situations where you don't want / can't 10 hard code the launcher activity this solution 9 works

Intent i = getPackageManager()
    .getLaunchIntentForPackage(getPackageName())
    .setPackage(null)
    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);

return new NotificationCompat.Builder(context)
        ...
        .setContentIntent(pendingIntent)
        .build();

The setPackage(null) part was key in my case since without it the 8 app did not resume to previous task. I compared 7 my intent with the intent from the Android 6 launcher and noticed that pkg was not set 5 there so that's how I came up with removing 4 package name from the intent.

My particular 3 situation was that the notification was 2 created in a library so there I could not 1 know what the launcher activity would be.

Score: 4

I also had the same problem and try to resolve 15 the problem like @GrAnd's answer:

final Intent notificationIntent = new Intent(context,YourActivity.class);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

This works, no 14 doubts about it but the problem is when 13 you set your intent as ACTION_MAIN. Then 12 you will not be able to set any bundle to 11 the intent. I mean, your primitive data 10 will not be received from the target activity 9 because ACTION_MAIN can not contain any 8 extra data.

Instead of this, you can just 7 set your activities as singleTask and call 6 your intent normally without setting ACTION_MAIN 5 and receive the intent from onNewIntent() method 4 of your target activity.

But beaware if you 3 call, super.onNewIntent(intent); then a 2 second instance of the activity will be 1 created.

Score: 3

Creating an activity and then set the categories 6 and a respective flags... This was the way 5 this worked for me, I had to do it this 4 way cause I did it to support Api lvl 8

intent.addCategory(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(this, YourActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT|
                Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 
                   PendingIntent.FLAG_UPDATE_CURRENT);

and in the 3 AndroidManifest

android:launchMode="singleTask"

So what made the trick was 2 the Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT along with the line setted in the manifest.

Hope 1 it helps to other people.

Score: 0

This is Very Simple open Your manifest file 2 and set attribute Launch mode singleTop in your activity 1 attribute

Score: 0

THE ULTIMATE SOLUTION: Notification to restore a task rather than a specific activity?

public class YourRootActivity extends Activity 
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
    
            if (!isTaskRoot()) // checks if this root activity is at root, if not, we presented it from notification and we are resuming the app from previous open state
            {
                 val extras = intent.extras // do stuffs with extras.
                 finish();
                 return;
            }
             // OtherWise start the app as usual
        }
    }

0

More Related questions