Note that there are some explanatory texts on larger screens.

plurals
  1. POMy service resets internal variables when opening ongoing notificiation
    text
    copied!<p>I have a setup where my activity starts a service and sends values through the intent that control how the service acts. These variables can change through the life of the service, and the service pushes updates back to the activity UI. If I exit the activity and relaunch it, it will request an update from the service to get the current values. This works fine if I re-launch the activity via the home screen, or if I use the recent apps menu. However if I launch the activity via the run in foreground ongoing notification I have set for the service, then the service re-sets itself to the original values when it was started.</p> <p>Here's my run in foreground code, I don't think there is anything else that would have an affect on this problem.</p> <pre><code>//Create notification and set to run in forground Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name), System.currentTimeMillis()); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, getText(R.string.app_name), "Running Noise Based", pendingIntent); startForeground(ONGOING_NOTIIFICATION, notification); </code></pre> <p>Update 3: </p> <p>Adding the flags Intent.FLAG_ACTIVITY_NEW_TASK, Intent.FLAG_ACTIVITY_CLEAR_TOP, and Intent.FLAG_ACTIVITY_SINGLE_TOP to the notification intent fixed the problem. I have no idea why these fixed it. If anyone can tell me why this happens please do. Here's the new code that works.</p> <pre><code>//Create notification and set to run in forground Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.app_name), System.currentTimeMillis()); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, getText(R.string.app_name), "Running Noise Based", pendingIntent); startForeground(ONGOING_NOTIIFICATION, notification); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload