android - 单击主屏幕小部件时如何打开特定 fragment

标签 android android-studio android-widget

如何在单击主屏幕小部件时打开特定 fragment ,我已经可以打开一个 Activity ,但我不知道如何从这是我的小部件类的小部件打开 fragment

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);
}

有什么想法吗?

最佳答案

为什么不将一些标志(Bundle)传递给 Intent 并在 Activity onCreate 或 onNewIntent 中检查它,如果设置为 true(或任何值)则打开 Fragment?

小部件:

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);

Activity :

Bundle extras = getIntent().getExtras();
if (extras != null) {
    value_type value = extras.get("key");        
}

请记住,您必须在 onCreate 和 onNewIntent 中检查它。

关于android - 单击主屏幕小部件时如何打开特定 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002168/

相关文章:

android - 如何在屏幕中央的所有小部件顶部显示 CircularProgressIndicator

android - 线性布局中的两个小部件一个挨着一个

java - 如何在 Android 中使用 Java 和 org.json 迭代这个 JSON 数组?

android-widget - 向 RemoteViews 动态添加自定义 View

Android TextureView 与 VideoView 性能对比

android - 错误 : failed to connect to camera service @ Android marshmallow

android-studio - 使Gradle忘记代理

java - 在 Android Studio 中添加依赖项是什么意思

android - RemoteViewsFactory 如何处理 Android 中的 ViewType?

java - onClick MediaPlayer 错误所有音频均已播放