2

I've a music player service and I'm showing a notification to users when it goes foreground. But when user click notification to come back to App, it's updating icons again (icons images are setted at onCreate). I know it's updating icons because it's recreating activity.

Here is a piece of code:

    public void startForeground(String songName){

        Intent showTaskIntent = new Intent(AudioPlayer.this, PlayerActivity.class);
        showTaskIntent.setAction(Intent.ACTION_MAIN);
        showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent.getActivity(
                this,
                0,
                showTaskIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle(getString(R.string.app_name))
                .setContentText(songName)
                .setSmallIcon(R.drawable.logo)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(contentIntent)
                .build();

        startForeground(NOTIFICATION_ID, notification);

I've tried many different flags, but it always update. Other related cases and solutions at stack don't helped me.

--

The main problem - explained:

  1. When you open the music player, you have play button, for example.
  2. You click play, it plays. Play image is changed for pause image.
  3. Activity goes foreground. It still playing.
  4. You click notification. Activity come back (onCreate, onStart, onResume). Then it changes pause image for play image.

    • If I click on App icon (System Icon not notification) it calls, onRestart. That is what I need, I think.

I hope someone can help me.

Alan Godoi
  • 657
  • 1
  • 12
  • 39
  • 1
    Could you please add `android:launchMode="singleTask"` to main activity in AndroidManifest.xml file ? – mdtuyen Nov 25 '15 at 07:23
  • Yep. It stopped changing. But it don't accepts pause commands. – Alan Godoi Nov 25 '15 at 14:24
  • @mdtuyen it worked. I found a bad test. But the problem of creating activity again was gone with your solution. Thank you. Please, add it as an answer, then I can mark it. Thanks again. – Alan Godoi Nov 25 '15 at 14:32

1 Answers1

3

Add android:launchMode="singleTask" to main activity in AndroidManifest.xml

mdtuyen
  • 4,470
  • 5
  • 28
  • 50