Android widget pendingintent lost after recovering from background

I'm writing the code for a widget (imagebutton) that can send and receive data sms, receiving a valid data sms will change it's icon and clicking again will reset it, i can have multiple instances of the same widget with different configuration on screen.

The problem is this: when the home screen restores from background, the widget stops responding to user clicks, but it's still detecting incoming data sms and updating the icon.

I think the problem could be the pendingIntent (I am not sure about the best flag to use in getBroadcast also) that it's created inside my updateAppWidget method:

Intent active = new Intent(context, AppWidget.class);     active.setAction(ACTION + String.valueOf(appWidgetId)); //action should look like this: "CLICKED_123"     PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, rc, active, 0); //rc is the first id of each single instance of the widget     views.setOnClickPendingIntent(, actionPendingIntent); 

I also discovered that when the widget goes on background it is destroyed and created again with different id, is this true? If it is, the problem could be related to my savedPreferences:

static void saveStatePref(Context context, int appWidgetId, int text) {     SharedPreferences.Editor prefs = context.getSharedPreferences(STAT_NAME, 0).edit();     // here i use the widget id to save my preferences     // so that i can tell how to update every single instance     prefs.putInt(PREF_PREFIX_KEY + appWidgetId, text);     prefs.apply(); } 

Is there a better way for saving preferences and giving request codes without relaying on their ids? I couldn't find anything similiar and i decided to post this question. Sorry for my english, feel free to edit.


Category: android Time: 2016-07-31 Views: 5

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.099 (s). 12 q(s)