Pergunta

When I'm adding single widget I can configure it properly and it's updated as supposed to after initial configuration. But when I add second and next instances of widget with the same or different configuration onUpdate is not triggered. Well, to be specific onUpdate is executed just before Configuration is set, when Configuration Activity is on top, so it works like:

1 widget - OK

2 widgets - First OK, Second not updated

3 widgets - First OK, Second OK, Third not updated

and so on.

Behavior tested on Android 4.3. I've no idea why update is not working as supposed to.

"OK" Button in Configuration Activity (straight from: http://developer.android.com/guide/topics/appwidgets/index.html#Configuring):

 public void ok(View v){

     Intent intent = getIntent();
     Bundle extras = intent.getExtras();
     if (extras != null) {
         mAppWidgetId = extras.getInt(
                 AppWidgetManager.EXTRA_APPWIDGET_ID, 
                 AppWidgetManager.INVALID_APPWIDGET_ID);
     }

     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
     RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_lay);
     appWidgetManager.updateAppWidget(mAppWidgetId, views);

     Log.e("mAppWidgetId", "id: "+mAppWidgetId);

     Intent resultValue = new Intent();  
     resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);  
     setResult(RESULT_OK, resultValue);  
     saveSettings();
     finish();  
 }

OnUpdate part in Widget Provider:

ComponentName thisWidget = new ComponentName(context, MyWidget.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {
            SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
            int myConfig = settings.getInt("myConfig"+widgetId, -1);
            if(myConfig != -1){
                Log.d("widget id", widgetId+" ");
                updateWidget(db, widgetId, myConfig);
            }
Foi útil?

Solução

This answer solved the issue: https://stackoverflow.com/a/5363553/1449961:

The problem was on NOT SENDING ACTION_APPWIDGET_UPDATE so before closing preferences I send broadcast:

Intent updateIntent = new Intent(this, CallBackWidget.class);
updateIntent.setAction("PreferencesUpdated");
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
sendBroadcast(updateIntent);

and in onReceive method of widget I check for broadcast:

if ("PreferencesUpdated".equals(action)) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
    appWidgetManager.updateAppWidget(appWidgetId, views);
    int[] appWidgetIds = new int[] {appWidgetId};
    onUpdate(context, appWidgetManager, appWidgetIds);
}  

but I feel like it's a hack rather than a good solution, so better way is still welcome.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top