AppWidgetManager getAppWidgetIds returning old Widget ids

后端 未结 1 580
我在风中等你
我在风中等你 2021-02-05 10:40

I\'m trying to get a list of all ACTIVE instances of my widget. In the OnUpdate method of my AppWidgetProvider, I\'m doing the following:

// Get all ids
Componen         


        
相关标签:
1条回答
  • 2021-02-05 11:24

    I ran into this as well. I'm guessing that getAppWidgetIds is simply programmed to always get all IDs ever associated with that widget provider. To solve this, I had to save the Widget ID to my shared preferences by saving the id in the onUpdate method and deleting it in the onDelete method of my AppWidgetProvider.

    public class MyProvider extends AppWidgetProvider {
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
            // Update your widget as normal
    
            // If the Id isn't already saved to your db/app preference, save it now
        }
    
        public void onDeleted (Context context, int[] appWidgetIds) {
            // Remove each id passed in through appWidgetIds 
            // "un-save" them from your db or app preferences
        }
    }
    
    0 讨论(0)
提交回复
热议问题