2

I am having a problem with resuming activity on notification click. I have an app that plays a single song for some time. The idea is when you play a song, and press Home button, there should be notification that returns you to the app where you can stop the song.
Here is how I am making the notification:

private void pushNotificationsOld()
{
    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager mNM;
    NotificationCompat.Builder builder;
    mNM = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setContentText("Now playing: " + songList.get(activeSong).name)
            .setOngoing(true);
    builder.setContentIntent(resultPendingIntent);
    mNM.notify(mId, builder.build());
}

And here is my Manifest:

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
                android:launchMode="singleInstance">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </activity>

pushNotificationsOld() is called by pressing the Play button.
And there is only one activity in the app.
With this code, when I press notification, new activity is created, and the music continues playing in the background.
When I press Home and get back to app from recent apps or icon, it worsk good.

filipst
  • 1,547
  • 1
  • 30
  • 55
  • There are a lot of questions like this, but I can't manage to fix this problem. – filipst Mar 09 '15 at 10:52
  • I have found the answer here: http://stackoverflow.com/questions/5538969/click-on-notification-starts-activity-twice?rq=1 Answered by Oderik. – filipst Mar 09 '15 at 11:12
  • this helped me http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather-than-making-a-new-intent/39482464#39482464 – TharakaNirmana Sep 14 '16 at 07:47

1 Answers1

1

In your manifest.xml, add for the MainActivity:

android:launchMode="singleTop"
Groco
  • 1,301
  • 15
  • 19