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:
- When you open the music player, you have play button, for example.
- You click play, it plays. Play image is changed for pause image.
- Activity goes foreground. It still playing.
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.