views:

57

answers:

2

I have a Widget i am working on, that allows the user to have more then one instance of the widget on his screen. Each Widget id maintains its own configuration file. However for some odd reason my code that is responsible for setting up the buttons individually for each widget id is not working, only the first widget id is linked to each individual widget. below is the code that is responsible. Can anyone see where the problem is?

private void TieClicks(Context context){
  RemoteViews rViews;
  PendingIntent editPendingIntent= null;

//  Intent updateintent = new Intent(context,SyncNoteWidget.class);
//  updateintent.setAction(SyncNote_Action_Widget_Update);
//  PendingIntent pendingupdateintent = PendingIntent.getBroadcast(context, 0, updateintent, 0);
//  rViews.setOnClickPendingIntent(R.id.widgettextview , pendingupdateintent);
//  
  AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  int[] ids = appWidgetManager.getAppWidgetIds(new ComponentName(context, SyncNoteWidget.class));
  for (int i =0;i< ids.length;i=i+1){
   int wId = ids[i];
   rViews = new RemoteViews(context.getPackageName(),R.layout.widget);

   editPendingIntent = makeControlPendingIntentActivity(context, wId);
   Log.v("syncnote", "tieing " + String.valueOf(wId));
   rViews.setOnClickPendingIntent(R.id.widgeteditbutton , editPendingIntent);
   appWidgetManager.updateAppWidget(wId, rViews);

   editPendingIntent= null;

  }
 }
 private PendingIntent makeControlPendingIntentActivity(Context context,int appWidgetId) {

     Intent active = new Intent(context, EditNote.class);
     active.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
     active.setAction(com.ntu.way2fungames.syncnote.SyncNoteWidget.SyncNote_Action_Edit); 
     active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     return(PendingIntent.getActivity(context, 0, active, 0 ));
    }
A: 

It would help if you included the log entries you're creating with: Log.v("syncnote", "tieing " + String.valueOf(wId)); That could answer the important question: How many values are in: int[] ids ?

What is in R.layout.widget ?

A picture of your widgets would help too. A picture is worth a thousand words...

androidSquakmtCom
The log output is as expected, if i have 2 widgets on my home screens then it will output something along the lines of tieing 112tieing 113and there are always as many values in ids as their are widgets. R.layout.widget is a simple linear layout with a text view and a button control. remember its just multiple copies of the same appwidget. Thanks for trying to help me out =)
nathan
A: 

The problem was that for some reason "PendingIntent.getActivity" was re-using the first PendingIntent.. God knows why. The fix is below, simply add some random data to the call.

private PendingIntent makeControlPendingIntentActivity(Context context,int appWidgetId) {
        Intent active = null;
        active = new Intent(context, EditNote.class);
        active.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        active.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK );
        active.setAction(com.ntu.way2fungames.syncnote.SyncNoteWidget.SyncNote_Action_Edit); 
        active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        PendingIntent pi =PendingIntent.getActivity(context,(int)(Math.random()*10000), active, 0 );
        Log.v("syncnote", "PI: "+pi.toString());
        return(pi);
    }
nathan