如何在点击主屏幕小部件时打开特定片段,我已经可以打开一个活动,但我不知道如何从小部件打开片段这是我的小部件类
public class MyWidget extends AppWidgetProvider {
ComponentName watchWidget;
RemoteViews remoteViews;
private static final String SYNC_CLICKED = "automaticWidgetSyncButtonClick";
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.My_widgets);
watchWidget = new ComponentName(context, MyWidget.class);
remoteViews.setTextViewText(R.id.appwidget_text,"Widget Example");
// start Activity from the widget
Intent configIntent = new Intent(context, MainActivity.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_container, configPendingIntent);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
所以任何想法?
答案 0 :(得分:5)
为什么不将一些标志(Bundle)传递给Intent并在Activity onCreate或onNewIntent中检查它,如果设置为true(或任何值),打开片段?
窗口小部件:
Intent configIntent = new Intent(context, MainActivity.class);
configIntent.putExtra("key", value);
configIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
的活动:
Bundle extras = getIntent().getExtras();
if (extras != null) {
value_type value = extras.get("key");
}
请记住,你必须在onCreate和onNewIntent中检查它。