I'm launching an activity on a buttonclick from AppWidget. The user enters some data on the activity and when user closes the activity, I want to update the data user entered in a TextView on AppWidget. Any idea how to do that? I have successfully launched activity from AppWidget, only problem being updating the AppWidget.
Asked
Active
Viewed 1,007 times
2 Answers
4
You can use RemoteViews
and the AppWidgetManager
:
AppWidgetManager manager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(
context.getPackageName(), R.layout.widget);
remoteViews.setTextViewText(R.id.textBoxId, "new textbox content");
manager.updateAppWidget(appWidgetId, remoteViews);

Josef Pfleger
- 74,165
- 16
- 97
- 99
-
In which method of my class extending AppWidgetProvider should I add this? – Amit Chintawar Jul 31 '10 at 15:46
-
The snippet will update a `TextView` in your widget. You wrote that you want to update your widget when the user closes an `Activity` so that `Activity`'s `onPause` method would be a good place for example. – Josef Pfleger Jul 31 '10 at 16:39
-
@AmitChintawar where does the appWidgetId come from ? – ChristopheCVB Jan 30 '12 at 21:19
0
You can call the appwidget update method from the host application activity or fragment.
This is how I have done it:
int[] ids = AppWidgetManager.getInstance(getApplication()).
getAppWidgetIds(new ComponentName(getApplication(), MyWidget.class));
MyWidget myWidget = new MyWidget();
myWidget.onUpdate(this, AppWidgetManager.getInstance(this),ids);

rdmc
- 21
- 5