Skip to content Skip to sidebar Skip to footer

Just Update A Widget Remoteviews Instead Of Completly Creating A New One?

So in my onUpdate method in my AppWidgetProvider class, I ended up executing a non-trivial amount of code so that I can completely recreate a new RemoteViews object. The reality is

Solution 1:

First, RemoteView is not a View. It's a set of instructions that build a View hierarchy. It is used to recreate a View in another process (App Widgets do not execute in your app's process). As such it's serializable and mutable.

So, when you initialize a RemoteView just store a reference to it somewhere, e.g. in a field of your custom AppWidgetProvider. Next time you need it, get it from field and the change something on it. For changing the string in a TextView use setString(..).

remoteView.setString(textViewId, "setText", "some text for TextView")

Solution 2:

This is the 2013 update if you are using current API's. In your WidgetProvider class' method that will perform an update:

AppWidgetManagerappWidgetManager= AppWidgetManager.getInstance(context);
rv = newRemoteViews(context.getPackageName(), R.layout.widgetlayout); 
rv.setTextViewText(R.id.ofTextViewInWidgetLayoutXML, "Hello World");
appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], rv);

Note that it is no longer remoteView.setString but remoteView.setTextViewText

Solution 3:

You can update the remote views and then call

ComponentName componentName= newComponentName(context, YourClass.class);
AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);

on, which AFAIK should update the widget

Post a Comment for "Just Update A Widget Remoteviews Instead Of Completly Creating A New One?"