On this question are not answers in all over internet, I not found. I spent all my day on searces, so I start to think, that nobody knows
Im new on stackoverflow and English is not my native lang. So, sorry for my bad English.
Ok, here is a question. I creating a simple widget with 2 buttons. One of there button may launch config.activity by on clicking it. How I can do it? Im try with Intent, as here:
Intent i = new Intent(context, ConfigActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
But when I receive the intent action I see next situation: config.activity quickly start and close, because the appWidgetId is not valid.
How to launch the configuration activity from widget?
you can use this code
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent launchActivity = new Intent(context, ConfigActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, launchActivity, 0);
titles.setOnClickPendingIntent(R.id.Button, pendingIntent);
ComponentName thisWidget = new ComponentName(context, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, rv);
If you are updating from Provider class, fetch the widget id from onUpdate() method, like this:
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
}
or if your updating widget from ConfigurationActivity, fetch the widget id from onCreate method like this:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
Make your widget lauch ConfigurationActivity from a button/imageview in your widget_layout
// Create intent pointing to ConfigurationActivity, in this example we are at ConfigurationActivity
Intent configurationIntent = new Intent(WidgetBarConfiguration.this, WidgetBarConfiguration.class);
// Create a extra giving the App Widget Id
configurationIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
// Create a pending intent giving configurationIntent as parameter
PendingIntent configurationPendingIntent = PendingIntent.getActivity(WidgetBarConfiguration.this, 0, configurationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Here we fecth the layout item and give it a action
RemoteViews views = new RemoteViews(WidgetBarConfiguration.this.getPackageName(), R.layout.widget_bar_layout);
// Setting onClick event that will lauch ConfigurationActivity
views.setOnClickPendingIntent(R.id.imageViewSettings, configurationPendingIntent);
// Updating widget
mWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
AndroidManifest.xml
<activity
android:name=".WidgetBarConfiguration"
android:label="@string/title_activity_widget_configuration"
android:launchMode="singleInstance"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
Look that lauchMode = "singleInstance"
will make sure only the ConfigurationActivity layout appear on clicked, without any other activity layout attached.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.