Updating Multiple Instances Of App Widget In Android
Trying to create multiple instances of app widget and update each of them separately, but can't find our why it's not working properly. Here is the code, collected from here and mo
Solution 1:
Based on what you've posted it seems that this:
// request for widget updatepushWidgetUpdate(context, remoteViews, appWidSingle);
should probably look like this instead:
// request for widget updatepushWidgetUpdate(context, remoteViews, appWidgetIds[appWidSingle]);
Likewise:
// register for button event
remoteViews.setOnClickPendingIntent(R.id.sync_button,
buildButtonPendingIntent(context, appWidSingle));
should be:
// register for button event
remoteViews.setOnClickPendingIntent(R.id.sync_button,
buildButtonPendingIntent(context, appWidgetIds[appWidSingle]));
Solution 2:
Besides the error mentioned by j__m this portion of code wasn't also correct:
publicstaticvoidpushWidgetUpdate(Context context, RemoteViews remoteViews, int appWidgetSingleId) {
AppWidgetManagermanager= AppWidgetManager.getInstance(context);
RemoteViewsviews=newRemoteViews(context.getPackageName(), R.layout.widget_main);
IntentclickIntent=newIntent(context, MyWidgetIntentReceiver.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetSingleId);
PendingIntentpendingIntent= PendingIntent.getBroadcast(context, appWidgetSingleId, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.sync_button, pendingIntent);
manager.updateAppWidget(appWidgetSingleId, views);
}
i.e was creating new remoteviews everytime instead of using passed remoteview in the pushwidgetupdate method. Hence above code is changed to:
publicstaticvoidpushWidgetUpdate(Context context, RemoteViews remoteViews, int appWidgetSingleId) {
AppWidgetManagermanager= AppWidgetManager.getInstance(context);
IntentclickIntent=newIntent(context, MyWidgetIntentReceiver.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetSingleId);
PendingIntentpendingIntent= PendingIntent.getBroadcast(context, appWidgetSingleId, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.sync_button, pendingIntent);
manager.updateAppWidget(appWidgetSingleId, views);
}
Post a Comment for "Updating Multiple Instances Of App Widget In Android"