The PendingIntent Cache Reuse And Multiple Android Widgets Issue

This was a pain to find out. While my Android widget was working okay considering that I only set it to have only 1 size, it took me a very long time to find out that the same PendingIntent object is used by the Android system when certain attributes are created the same way e.g. class, action, request code.

The quickest and easiest solution is to use the appWidgetId as the value of the request code in the PendingIntent parameter when you either call the getActivity(), getBroadcast() or getService() methods.

That should make it unique in such a way that the PendingIntent object will be used only for that certain widget no matter which size or which instance it is.

Other options would be to give different values for its setData() and setAction() methods like adding a System.currentTimeMillis() but sometimes we may want our action value to be fixed so the best option in my opinion is to focus on the request code value having a unique value.

That should give make your multiple widget instances work.

Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Your email address will not be published. Required fields are marked *