AleCat83
AleCat83

Reputation: 1493

Android: configuring widget (same setOnClickPendingIntent extras for different widgets instance)

I'm doing some android exercise and I have a problem with widgets:

The goal: when the user adds a new widget to the home the application run a configuration where the user can choose a string. During the configuration that string must be set as "text" of the only button inside my widget layout and on that button the application must set an onclick listener that will call another activity passing the string as parameter. So when the user click the button will call the activity giving the button text. The main goal is that if I have two widgets, each one set the onclick listener sending a different string as parameter.

The code:

int mAppWidgetId = 0;

if (extras != null) {
   mAppWidgetId = extras.getInt(
   AppWidgetManager.EXTRA_APPWIDGET_ID, 
   AppWidgetManager.INVALID_APPWIDGET_ID);
 }

String thestring = ... //this string is set dynamically by the configuration


RemoteViews views = new RemoteViews(this.getPackageName(), R.widget_profile);


    views.setTextViewText(R.id.buttonWidget, thestring);


    Intent clickIntent = new Intent(this, afterclickactivity.class);
    clickIntent.putExtra("TheString", thestring);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, clickIntent, 0);
    views.setOnClickPendingIntent(R.id.buttonWidget, pendingIntent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
    appWidgetManager.updateAppWidget(mAppWidgetId, views);

The problem: everythings seems to work fine, the widget get place in the home screen and the text on the button is the one choosen by the user and if I add 2 instance of the same widget they have different text according to the user selection. But when the user press the button on he second widget it launch "afterclickactivity" with the same parameter as widget1 ignoring the second choosen string.

It is like:

Widget 1: text: string1 onclick: callActivity(string1)

Widget 2: text: string2 onclick: callActivity(string1)

Upvotes: 1

Views: 639

Answers (2)

I was having the same problem and found the solution on this link http://www.bogdanirimia.ro/android-widget-click-event-multiple-instances/269

Here is the solution: Android reuses intents, so when you create an intent, make sure you put an unique ID, or else the same intent used before will be triggered for all instances! Only with this detail I spent half a day! ( PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, clickIntent, 0); )

Upvotes: 1

Pavel Bazika
Pavel Bazika

Reputation: 406

It is caused by the way how Intents are compared. PendingIntent is registered in system and when you try to add another one, check for equality with those already registered is performed. Contained Intent is compared using Intent.filterEquals, which checks action, data, type, class, and categories of the Intent. If these are the same for the already registered Intent and the new one, PendingIntent is not duplicated. It is either replaced or left (depends on parameters, your choice).

Note, that extras are not compared here.

So you will need to somehow differentiate the intents, for example by using data field or request code for example.

See documentation about PendingIntent here: http://developer.android.com/reference/android/app/PendingIntent.html

And related documentation about Intent itself

Pavel

Upvotes: 5

Related Questions