I have implemented a App Widget to launch my activity when clicked.
onUpdate()
method of WidgetProvider
:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidgetprovider_layout);
// ....update updateViews here
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
Intent onClickedIntent = new Intent(context,MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, onClickedIntent, 0);
updateViews.setOnClickPendingIntent(R.id.myView, pi);
appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}
}
It work as expected after the widget added on home screen.
But after sometimes, it cannot launch the activity again! I have to remove the widget and add again.
How can I fix it? please help.
I'd do it like this:
One thing I'm not sure on is the call to
super.onUpdate()
. My own widget code doesn't have it and seems to work fine... not sure if it's needed or not.I don't know if this refactor will fix your issue though!