Update issue of an app widget using collection

前端 未结 3 1492
伪装坚强ぢ
伪装坚强ぢ 2021-02-20 11:55

I have created an app widget using collection for my app, The widget shows date and list of items on that particular date. Everything works fine and the widget is updating as re

3条回答
  •  无人共我
    2021-02-20 12:14

    When you create the PendingIntents in updateAppWidget, for each widget, you create these two:

    PendingIntent nextButtonPendingIntent = PendingIntent.getBroadcast(context, widgetId, nextButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent prevButtonPendingIntent = PendingIntent.getBroadcast(context, widgetId, prevButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    For both, you set the second argument to widgetId. The PendingIntent system doesn't look to see if the Intent argument is different. (In fact, it definititely shouldn't do this - otherwise you couldn't update an existing PendingIntent to a new Intent.) This means that nextButtonPendingIntent and prevButtonPendingIntent end up the same.

    The solution is to put known, distinct, numbers in that argument and to put other useful information (like the widget Id) inside the intent:

    nextButtonIntent.putExtra("widget_id", widgetId);
    

    And to retrieve it in the onReceive():

    int widgetId = intent.getIntExtra("widget_id");
    

提交回复
热议问题