user959631
user959631

Reputation: 992

Widget ID and Intents

I have set a widget up with AppWidgetManager, it has a configure activity, once I have pressed done on the activity, based on my selection the widget's appearance changes. And when the I tap on the widget, the configure activity is shown again. That's all fine and works, but if I kill the widget, using a third-party task manager, or Android's task list, (when you press the home button, and a list of apps appear), swiping away the configure activity, something really odd happens. I can tap on the Widget, and the configure activity shows, but when I press done on there, nothing happens on the widget. I need to remove the widget for it to work.

//Somewhere at the top (global variable)
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

....

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
    mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
}

if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultValue);
}

finish();

....
//Then in my Widget, I set the PendingIntent for tapping the RemoteViews
Intent intent = new Intent(oContext, Widget_Configure.class);

for (int c = 0; c < oAppWidgetIds.length; c++){
     rv.setInt(R.id.widget_relative_layout, "setBackgroundColor", Color.argb(alpha, red, green, blue));             
     PendingIntent pendingIntent = PendingIntent.getActivity(oContext, oAppWidgetIds[c], intent, PendingIntent.FLAG_UPDATE_CURRENT );                           

     rv.setOnClickPendingIntent(R.id.borderBottomLeft, pendingIntent);
     rv.setOnClickPendingIntent(R.id.borderBottomRight, pendingIntent);
     rv.setOnClickPendingIntent(R.id.borderTopLeft, pendingIntent);
     rv.setOnClickPendingIntent(R.id.borderTopRight, pendingIntent);
     rv.setOnClickPendingIntent(R.id.widget_relative_layout, pendingIntent);

     oAppWidgetManager.updateAppWidget(oAppWidgetIds[c], rv);
}

//oContext is global Context, when Widget starts oContext is set to `this`;

How can I make it so that, I don't have to remove the widget, even though the widget is killed, and make it restart itself, if possible.

Thanks.

Upvotes: 0

Views: 121

Answers (1)

user959631
user959631

Reputation: 992

Nevermind, I figured it out myself, thanks anyways.. =]

EDIT: HOW I DONE IT...

I had running worker threads on the Widget, and using a task manager stopped them. So I realised I needed to restart the Widget, (call the OnUpdate method (Restart because my widget would start the thread and set-up AppWidgetManager and other stuff)

Seeing as though my configure activity would start regardless of the fact that the widget was killed, I just forced the widget to update in the OnClick event of the button.

I needed to save the AppWidgetIds that are created when the OnUpdate method is first called of the Widget.

This is the code in the OnClick event of the button:

Intent updateWidgetIntent = new Intent(Context, <WIDGET_CLASS>.class);
updateWidgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int ids[] = {mAppWidgetId};
updateWidgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(updateWidgetIntent);

Where mAppWidgetId is an array of AppWidgetIds (that where previous saved).

I hope this makes sense...

Upvotes: 1

Related Questions