0

I am launching an Activity from a home screen Widget. When doing this I want to pass configuration data from the Widget to the Activity. So I added extra data to the Intent, which works fine. The problem comes when I add more than one Widget with different configurations. Regardless of which of the Widgets I tap, I always get the configuration of the Widget "instance" that was last added. Below is some test code I did to test this, only passing the appWidgetId to the Activity. The Activity should display the appWidgetId of the Widget that was tapped.

public class TestWidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,    int[] appWidgetIds) {
        for (int i=0; i<appWidgetIds.length; ++i) {
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
    }

    private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        updateViews.setTextViewText(R.id.text_view, "Id "+appWidgetId);

        Intent intent = new Intent(context, TestActivity.class);
        intent.putExtra("appWidgetId", appWidgetId);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);       

        updateViews.setOnClickPendingIntent(R.id.text_view, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, updateViews);
    }
}

and the Activity is simply

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        TextView textView = (TextView)findViewById(R.id.app_text_view);
        textView.setText("appWidgetId " + getIntent().getIntExtra("appWidgetId", -1));
    }
}

In the Intent I use the flag FLAG_UPDATE_CURRENT, and there is no science behind this. First I had this as 0, but the flag seem to change the behavior. Without the flag I get the ID of a previous Widget, with the flag I get the ID of the last added (I think).

From the code it is quite obvious that this will not work. The method setOnCkickPendingIntent is just called several times without specifying which Widget instance (appWidgetId) that it refers to.

Is there a way to do this so that the application can get configuration data from the Widget? At least it should see the appWidgetId of the widget that has been tapped.

Thanks for your help!

vidulaJ
  • 1,232
  • 1
  • 16
  • 31
Anders625
  • 1
  • 1
  • The question title of the duplicate is different, but the source of the problem here is that all PendingIntents use the same Intent as well as *the same request code*. So they *are* the same Pending Intent. – Bö macht Blau Oct 27 '16 at 15:53
  • Tanks! I just set the appWidgetId as requestCode in the PendingIntent and it solved it. – Anders625 Oct 28 '16 at 16:10

0 Answers0