I am wondering about the onUpdate
method. I know that it is called the first time a widget is placed on the homescree, and then period in the time I defined in the appwidget info xml file.
But it seems as if this method is also called whenver I make changes to my AppWidgetProvider
class, ie. adding or removing method. Is that observation correct? I don't understand how this triggers the onUpdate
method?
public class ExampleAppWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
String buttonText = prefs.getString(KEY_BUTTON_TEXT + appWidgetId, "Press me");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_widget);
views.setOnClickPendingIntent(R.id.example_widget_button, pendingIntent);
views.setCharSequence(R.id.example_widget_button, "setText", buttonText);
Bundle appWidgetOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
resizeWidget(appWidgetOptions, views);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}