Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple OnClick on Widget for the same Intent

My Widget starts a Service and the Service Update a List of 3 LinearLayouts.

I want to set on each LinearLayout a SetOnClickPeningIntent with different Extra

But when i start the widget and want to click on the LinearLayouts only the last one is onclickable =/ I dont know what's wrong. Hope you can help me.

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


for (int widgetId : appWidgetIds) {


    int[] lls = { R.id.ll_con_1, R.id.ll_con_2, R.id.ll_con_3 };


        for (int i = 0; i < jray.length(); i++) {

                    try {

                        JSONObject o = jray.getJSONObject(i);

                        //Onclick
                            if(i == 0)
                            {
                                Intent msg_intent = new Intent(getApplicationContext(), MSGsOpenMsg.class);
                                msg_intent.putExtra("messageid", o.getString("id"));
                                PendingIntent msg_pendingIntent = PendingIntent.getActivity(
                                        getApplicationContext(), 0, msg_intent, Intent.FLAG_ACTIVITY_NEW_TASK);
                                remoteViews.setOnClickPendingIntent(R.id.ll_con_1, msg_pendingIntent);
                            }
                            else if(i == 1)
                            {           
                                Intent msg_intent1 = new Intent(getApplicationContext(), MSGsOpenMsg.class);
                                msg_intent1.putExtra("messageid", o.getString("id"));
                                PendingIntent msg1_pendingIntent = PendingIntent.getActivity(
                                        getApplicationContext(), 0, msg_intent1, Intent.FLAG_ACTIVITY_NEW_TASK);
                                remoteViews.setOnClickPendingIntent(R.id.ll_con_2, msg1_pendingIntent);
                            }
                            else if(i == 2)
                            {
                                Intent msg_intent = new Intent(getApplicationContext(), MSGsOpenMsg.class);
                                msg_intent.putExtra("messageid", o.getString("id"));
                                PendingIntent msg2_pendingIntent = PendingIntent.getActivity(
                                        getApplicationContext(), 0, msg_intent, Intent.FLAG_ACTIVITY_NEW_TASK);
                                remoteViews.setOnClickPendingIntent(R.id.ll_con_3, msg2_pendingIntent);
                            }

                    } catch (JSONException e) {

                    }
        }

}
like image 712
Stefan Avatar asked Jul 31 '11 19:07

Stefan


1 Answers

I've had this problem. It's because Android tries to collapse similar PendingIntents into 1 one and this has some really annoying consequences. IE the older intents are dropped when they are subtly different. I think there's also a bug in how they do collapsing as well since it doesn't match the documentation. (I ran into this a long time ago so things could have changed.)

Basically I make sure that the requestCode parameter in PendingIntent.getActivity is different for each UI item. That way they each get their own PendingIntent and never collide.

I have code here that tries to get around the problem but it's a bit wasteful. http://code.google.com/p/futonic-mylocationwidget/source/browse/src/com/futonredemption/mylocation/Intents.java#35

I will admit the solution in my code isn't the best but it seems to work pretty well so far.

like image 193
Jeremy Edwards Avatar answered Nov 12 '22 06:11

Jeremy Edwards