• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problems with multiple widgets in a single app

Amanoo

Newbie
I have multiple widgets provided by one app, and thus multiple classes that extend AppWidgetProvider. The idea is that every widget contains a Button, which starts a Service when pressed. This Service runs a bit of code and then stops. Now for the problem: for example, I have a Widget1a and a Widget1u. These widgets look like each other, but each widget does a slightly different thing. If I place a Widget1a on the homescreen and press it, a Service is started and the code that this Service should run is run, as it should be. Now I place a Widget1u on the homescreen, and suddenly Widget1a stops working and doesn't do anything when I press it. If I place a second Widget1a on the homescreen both Widget1as work again, but now Widget1u is disabled and doesn't respond when I press it. In short: it seems that at any particular time only one type's code works when it is pressed (and if there are multiple instances of the same type on the homescreen all of these instances will work, but no instances of any other widget of my app will do anything when pressed).

The code in the AppWidgetProvider of each widget is pretty basic. It merely contains an overrided onUpdate-method. It creates an Intent to start a Service that I previously created, this is then used to create a PendingIntent which is used by setOnClickPendingIntent to make sure it can be activated by a Button in the widget. Each widget makes, when its Button is pressed, a slightly different Intent so that a certain Service is executed slightly differently. So this only works for the instances of 1 widgettype at a time, namely those of which an instance was added to the homescreen most recently.

Why do all my app's other widgets stop working when I add a widget of a different type to my homescreen? Does anyone know more about widgets or how I can make sure that each widget keeps doing its job when I add a new one from the same app to the homescreen?

Here is the onUpdate method of one of my AppWidgetProviders for one of my widgets. All widgets operate similarly and I know I used a lot of copypasting which is a bad programming habit but it didn't seem to really want to work otherwise.
Code:
@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // Create an intent to launch the service
        Intent serviceIntent = new Intent(context, SendService.class);

        //I just put some extra data here to be used by the Service which wil eventually get this Intent and thus the data inside
        serviceIntent.setData(Uri.parse("uri::somethingrandomandunique"));
        serviceIntent.putExtra("Lamp", "1a");
        
        // PendingIntent is required for the onClickPendingIntent that actually
        // starts the service from a button click
        PendingIntent pendingServiceIntent = 
            PendingIntent.getService(context, 0, serviceIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

        // Get the layout for the App Widget and attach a click listener to the
        // button
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1a);
        views.setOnClickPendingIntent(R.id.button1a, pendingServiceIntent);
     //   super.onUpdate(context, appWidgetManager, appWidgetIds);
        ComponentName componentName = new ComponentName(context.getPackageName(), Widget1a.class.getName());

        appWidgetManager.updateAppWidget(componentName, views);
    }
So what am I doing wrong?
 
Back
Top Bottom