Just update a widget RemoteViews instead of completly creating a new one?

前端 未结 3 1717
傲寒
傲寒 2021-02-10 07:35

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

3条回答
  •  旧巷少年郎
    2021-02-10 07:58

    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")
    

提交回复
热议问题