Notification always creates new activity instead of resuming previous activity

android notification click open new activity
open activity from notification android
how to move to specific activity on notification click in android
handle notification click android
android notification pendingintent
notification click event android

I'm making a music app which displays a notification while audio is playing. When clicked, this notification opens the main (UI) activity through an intent.

While this should be a pretty simple process, for some reason no matter what I do the main activity is always destroyed when the notification is pressed. I've tried singleTop, singleTask (both as intent flags and manifest values), saving instance state bundles, onNewIntent, basically anything close to a solution I can find. But the activity is ALWAYS destroyed. I can only get the intent through getIntent.

Main Activity:

Audio Service:


since "links to pastebin must be accompanied by code"

here's the intent (i've tried every combination of relevant flags so I really doubt that's the issue)

// creates an intent which opens the application when the notification is pressed
        private PendingIntent getPendingIntent(Context context) {
            Intent intent = getIntent(this);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // returns an intent which resumes the Main Activity and passes various song information
        private Intent getIntent(Context context) {
            int duration = player != null ? player.getDuration() : 0;
            boolean playing = player != null && player.isPlaying();

            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra(MediaPlayerContract.INTENT_EXTRA_SONG_INFO, getSongInfo());
            intent.putExtra(MediaPlayerContract.INTENT_EXTRA_POS, currentQueuePos);
            intent.putExtra(MediaPlayerContract.INTENT_EXTRA_DURATION, duration);
            intent.putExtra(MediaPlayerContract.INTENT_EXTRA_IS_PLAYING, playing);
            return intent;

and here's where i'm trying to read the data

protected void onNewIntent(Intent intent) {
    Log.e(LOG_TAG, "flag got intent");
    String[] info = intent.getStringArrayExtra(MediaPlayerContract.INTENT_EXTRA_SONG_INFO);
    int pos = intent.getIntExtra(MediaPlayerContract.INTENT_EXTRA_POS, 0);
    int duration = intent.getIntExtra(MediaPlayerContract.INTENT_EXTRA_DURATION, 0);
    currentQueuePos = pos;

Try to remove flag: Intent.FLAG_ACTIVITY_CLEAR_TOP and add instead next one: Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

Start an Activity from a Notification, To start a "regular activity" from your notification, set up the PendingIntent using TaskStackBuilder so that it creates a new back stack as follows. This is an activity that exists as a part of your app's normal UX flow. So when the user arrives in the activity from the notification, the new task should include a complete back stack, allowing them to press Back and navigate up the app hierarchy. Special activity The user only sees this activity if it's started from a notification.

You can use launch modes for an activity.

<activity android:name=".MainActivity"

singleTop: If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

Introduction to Activities, An Activity is an application component that provides a screen with which users can in that a user's interaction with the app doesn't always begin in the same place. Instead, the user journey often begins non-deterministically. In fact, activities often start up activities belonging to other apps. Last updated 2019-12​-27. It will open a new activity . Fill your Contact Number and Address or click back button. After click back button, back the previous activity without refresh or reload. If you are fill your Father Name and Mother name then click Next button . Now a new activity will be open. In Back button click the previous activity will be open without refreshing and

I've solved the problem. I needed to call the setSessionActivity() method on my media session, like so:

Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, MediaPlayerContract.REQUEST_CODE,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);


you can also create an intent by simply calling


after calling setSessionActivity, where controller is your MediaSessionController.

Android Activity Launch Mode, It instructs how any new activity should be associated with the current task. Back button, the current activity is destroyed and the previous activity resumes. In this launch mode a new task will always be created and a new instance will be the current task, then instead of launching a new instance of that activity, all of the  Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

Preserving Navigation when Starting an Activity, Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling  Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the Back button always takes the user to the previous activity. However, if you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground.

Pausing and Resuming an Activity, For example, when a semi-transparent activity opens (such as one in the style of a dialog), the previous activity pauses. As long as the activity is still partially  If the previous notification has been dismissed, a new notification is created instead. You can optionally call setOnlyAlertOnce() so your notification interupts the user (with sound, vibration, or visual clues) only the first time the notification appears and not for later updates. Caution: Android applies a rate limit when updating a

Android application and activity life cycle - Tutorial, Exercise: Tracing the life cycle with notifications. 3.1. Create project; 3.2. It is also always the last component of the application, which is terminated. This object Used to initialize the activity, for example create the user interface. onResume() Called once another activity gets into the foreground. Always  This will ensure that if the activity is already running, it will bring it to foreground when notification is clicked instead of creating a new instance. Pheww!! Everything is done…