1

I have an application with an AppWidget. The AppWidget has some buttons which when clicked must open an Activity. The Activity must change some object's values depending on the appWidgetId of the AppWidget that has started this Activity. Here's a piece of code from the AppWidgetProvider subclass:

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

    for (int id : appWidgetIds) {

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget);

        Intent localCodeIntent = new Intent(context,
                CountrySelectorActivity.class);

        localCodeIntent
                .setAction(CurrencyRatesWidgetHelper.WIDGET_UPDATE_LOCAL_CURRENCY);
        localCodeIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
        PendingIntent localCodePI = PendingIntent.getActivity(context, 0,
                localCodeIntent, 0);
        views.setOnClickPendingIntent(R.id.local_iv, localCodePI);

The Activity starts well, but when I'm trying to extract the appWidgetId, I get the value of 1 everytime. Here's the Activity's onCreate() method code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    appWidgetId = intent
            .getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
    action = intent.getAction();

I'm sure I'm making a mistake somewhere, but can't really figure it out. Thanks in advance.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • What value do you see when you print out the id from the `onUpdate` method? If that is also `1`, then the id is `1`. Otherwise, you may need to use http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT to stop the Intent being reused (and possibly sharing a single widget id). Another trick to avoid caching of Intent is to put a unique random value in the Intent, e.g. `localCodePI.putExtra("random", Math.random())`. – Paul Grime Feb 15 '12 at 11:02
  • @PaulGrime, I've tried both things, but the problem doesn't seem to be solved. Maybe, I misunderstand the mechanism of PendingIntents? Are they being created one for each widget when it's being created? – Egor Feb 15 '12 at 11:26
  • Could you try adding the random to `localCodeIntent` instead of `localCodePI`? I think I advised you incorrectly. Maybe you could also print the hashcode of the received Intent in the Activity to see if they are being shared? – Paul Grime Feb 15 '12 at 11:29
  • 1
    @PaulGrime, I think I've figured out the things. I've tried using FLAG_UPDATE_CURRENT and now I'm receiving updates for the latest widget I've created, no matter what widget did I click on. Is there any way to create a unique PendingIntent for one single Activity but with different extras? – Egor Feb 15 '12 at 11:33
  • @PaulGrime, P.S. The "random" trick doesn't seem to work.. I've added it to Intent actually. – Egor Feb 15 '12 at 11:34
  • @PaulGrime, OK, found the solution. Here's the link, if you might be interested: http://stackoverflow.com/questions/3730258/mulitple-instances-of-pending-intent. Thanks for your help! – Egor Feb 15 '12 at 11:38

0 Answers0