AppWidgets заменяются фантомными виджетами после обновления приложения - PullRequest
0 голосов
/ 18 апреля 2020

Пользователи могут создать список виджетов в моем приложении, и тогда каждая запись будет отображаться в отдельном виджете приложения.

Каким-то образом обновления приложения приводят к тому, что виджеты исчезают на некоторых устройствах (Xiaomi) и, кажется, заменяют их с фантомными / призрачными виджетами.

Добавление новых виджетов в мой список все еще работает, если я добавлю новые виджеты на домашний экран тоже. Но я должен сохранить старые в списке. В противном случае новые будут отображать серое сообщение об ошибке android «проблема загрузки виджета».

Пример:

Before Update:
widget1 -> widgetInfo1
widget2 -> widgetInfo2

After Update:
widget1 -> completely gone
widget2 -> completely gone

After adding new widgets to the homescreen and the infoList:
widget1 -> 'problem loading widget'
widget2 -> 'problem loading widget'
widget3 -> widgetInfo3
widget4 -> widgetInfo4

If deleting widget1 and widget 2 from the homescreen:
widget3 -> 'problem loading widget'
widget4 -> 'problem loading widget'

Однажды у меня была похожая проблема, когда я изменил имя пакета.

Это сокращенное onUpdate от AppWidgetProvider:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    //loading the information about my widgets
    //WidgetInfo is a class that defines how the widget is supposed to look
    //each list entry contains info about one widget
    List<WidgetInfo> widgetInfos = new ArrayList<>();
    widgetInfos = getWidgetInfosFromDatabase();

    //int[] appWidgetIds is unreliable
    int[] myIds = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, AppWidget.class));
    //id's don't come sorted sometimes
    Arrays.sort(myIds);

    //the position within the myIds array
    int widgetPosition = 0;
    //the position within List<WidgetInfo>
    int InfoId = 0;

    //looping through all widgets and all widgetInfos
    //stopping when one of them reaches the end
    while (widgetPosition < myIds.length && InfoId < widgetInfos.size()){

        //the user can exclude list entries from being displayed
        //in this case the infoId gets increased but the widgetPosition stays the same
        //then the loop jumps to the next iteration
        if (widgetInfos.get(InfoId).isSkipped()) {
            InfoId++;
            continue;
        }

        //changing the widgets views with the information from MyWidgetInfo
        MyWidgetInfo myWidgetInfo = widgetInfos.get(InfoId);
        //RemoteViews views = new RemoteViews ...
        //views.setint ... myWidgetInfo.getText ...

        //updating this widget
        appWidgetManager.updateAppWidget(myIds[widgetPosition], views);

        widgetPosition++;
        InfoId++;
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}
...