How to start Activity from Android AppWidget?

oleg.semen picture oleg.semen · Jun 30, 2012 · Viewed 8.8k times · Source

Code like this works well.

    Intent configIntent = new Intent (context, WidgetConfigActivity.class);
    configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteView.setOnClickPendingIntent(R.id.btn, pIntent);

But I want to hide that button befor activity will appear, so I'm triing to send intent to the widget itself, perform hiding components in onReceive() method and then start activity. Problem is that I can't use startActivity() function in AppWidget.

Is there any solution ?

Answer

CommonsWare picture CommonsWare · Jun 30, 2012

Problem is that I can't use startActivity() function in AppWidget.

Yes, you can. You are passed in a Context object into onUpdate() (or onReceive()) of your AppWidgetProvider -- call startActivity() on that.