0

In the sample application, it does 4 actions, Action 1~4. I want to let users create widget buttons for each different action. When the user creates a widget, the configuration activity allows the user to choose one action. Pressing the widget should do the action.

The problem is that when a widget for Action 1 is created and then another for Action 2 is created, the widget action for Action 1 seems to be overridden by that of Action 2. That is, any widget works like the last widget. How can I fix this? I have uploaded the full project to http://tempsend.com/E552810CFE (valid until August 13, 2014).

public class WidgetReceiver extends AppWidgetProvider
{
public WidgetReceiver()
{
}

private static final String WIDGET_BUTTON_CLICKED = "WidgetButtonClickedIntent";

@Override
public void onReceive(Context context, Intent intent)
{
    super.onReceive(context, intent);

    if (WIDGET_BUTTON_CLICKED.equals(intent.getAction()))
    {
        int actionID = intent.getIntExtra("ActionID", -1);
        Toast.makeText(context, "Doing action " + actionID, Toast.LENGTH_SHORT).show();
        Log.d("StackOverflow", "Doing action " + actionID);
    }
}

@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
                                      int appWidgetId, Bundle newOptions)
{
    int actionID;

    super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    Log.d("StackOverflow", "onAppWidgetOptionsChanged is called for " + appWidgetId);


    Bundle bun = appWidgetManager.getAppWidgetOptions(appWidgetId);
    actionID = bun.getInt("ActionID");
    Log.d("StackOverflow", "The action ID is " + actionID);


    RemoteViews remoteViews;
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_button);

    remoteViews.setTextViewText(R.id.textView, "Action " + actionID);
    remoteViews.setOnClickPendingIntent(R.id.textView,
            getPendingSelfIntent(context, WIDGET_BUTTON_CLICKED, actionID));

    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    String ids = Arrays.toString(appWidgetIds);
    Log.d("StackOverflow", "onUpdate is called for " + ids);

}

protected PendingIntent getPendingSelfIntent(Context context, String action, int actionID)
{
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    intent.putExtra("ActionID", actionID);

    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

}

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

0

I have found a solution and it worked. Android widget not updating - has incorrect widget ID The second parameter was "requestCode", and I thought it was irrelevant because of its name. According to the answer, the Pending Intent needs to be unique, so I need to do anything to make it unique such as putting a unique request code, which in my case the widget ID. I am not sure if this is the clean solution. If someone knows a better, correct solution please add that here. Then I will choose it as the answer.

Community
  • 1
  • 1
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135