Android app will not be updated from activity

I have a simple appwidget, and I want to update it when an action occurs in action (in the same application). in onUpdate (), I immediately update the widget, which works fine. In my activity, I call the same static update method in my application, which is called in onUpdate () to update the views. widget is not updating.

I can trace the code directly in the AppWidgetManager.updateAppWidget () method, and all this is good, but the widget does not update.

The only possible difference that I see is that the context object passed to my static update method is different when it is called from the activity context or the context of the appwidget onUpdate () method. however there are many examples on the Internet, so I expect this to work.

+8
android android-appwidget
source share
2 answers

Without seeing my code, I am not 100% sure how you are trying to do this, but I am using this method. In my Activity , I have the following method:

 private void updateAllWidgets(){ AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext()); int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, MyWidget.class)); if (appWidgetIds.length > 0) { new MyWidget().onUpdate(this, appWidgetManager, appWidgetIds); } } 

Where MyWidget is the appwidget class. I can call this method from anywhere in my Activity to update all my applications.

+19
source share

Instead of using the static method, take advantage of the fact that the widget is already a broadcast receiver and registers the intention of updating for it in your manifest. Then when you need to update it from activity, just call

 //in your activity sendBroadcast(new Intent(MyWidget.ACTION_UPDATE).putExtra(whatever)); //In widget class public static final String ACTION_UPDATE = "com.example.UPDATE_MY_WIDGET"; 

and inside the receiver tags of your manifest file

 <intent-filter> <action android:name="com.example.UPDATE_MY_WIDGET"/> </intent-filter> 
+9
source share

Source: https://habr.com/ru/post/650075/


All Articles