Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Activity from Android AppWidget?

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 ?

like image 237
oleg.semen Avatar asked Jun 29 '12 22:06

oleg.semen


2 Answers

Thanks 2 CommonsWare

There is one more thing to do. context.startActivity(); throws RuntimeException in this case.

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

So you need to set flag

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

before.

like image 56
oleg.semen Avatar answered Oct 19 '22 17:10

oleg.semen


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.

like image 19
CommonsWare Avatar answered Oct 19 '22 16:10

CommonsWare