In a service I have a main remoteview
notRemoteView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
and to that I add another remoteview which contains an ImageButton
RemoteViews btnView1 = new RemoteViews(context.getPackageName(), R.layout.btn1);
notRemoteView.addView(R.id.image_button_container, btnView1);
after that I call:
startForeground(requestCode, notification);
It all works fine and does the appropriate action when clicked but then the ImageButton gets duplicated. The btnView1 remoteview seems to get re-added to the main remoteview. With each button click another button is added to the notification. This happens even when I null both of the remotveiws before building the notification.
I used to have the ImageButton as part of the main remoteview originally and it worked perfectly fine.
Note: I use startForeground(requestCode, notification);
with the same requestCode, is this the right way to update service/background notification
I figured it out, I needed to remove all remoteviews each time I wanted to update notification:
notRemoteView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
notRemoteView.removeAllViews(R.id.image_button_container);
Thanks to this answer: Android app widget: content added twice