Widget not deleted when passing RESULT_CANCELED as result for configuration activity

后端 未结 4 1582
庸人自扰
庸人自扰 2020-12-24 03:07

I have some problems with my widgets. Here is the description:

Context:

I have a home widget.

When I add it, it pops a configuration

4条回答
  •  不知归路
    2020-12-24 03:39

    I solved this problem like this,

    in the widget provider's onUpdate() method I check if the widget was configured, and if it wasn't I don't do anything, so no ghost widgets. At the end of the configuration I just set it to true and I'm good to go. Just don't forget to remove it from sharedpreference when the widget is deleted.

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    
        for(final int appWidgetId : appWidgetIds)
        {
            final StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(appWidgetId);
            stringBuilder.append("_conf");
            if(context.getSharedPreferences("settings",0).getBoolean(stringBuilder.toString(),false)) 
                updateAppWidget(context,appWidgetId,appWidgetManager);
    
        }
    
    
    }
    

提交回复
热议问题