android - 从 Android 7.1 应用程序快捷方式启动 fragment (而不是 Activity )

标签 android android-fragments android-manifest android-7.1-nougat app-shortcut

我决定考虑将静态快捷方式添加到应用程序中,使用此页面作为引用:

https://developer.android.com/preview/shortcuts.html

我的快捷方式的 XML 目前看起来是这样的:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="id"
        android:enabled="true"
        android:icon="@drawable/icon"
        android:shortcutShortLabel="@string/short_label"
        android:shortcutLongLabel="@string/long_label"
        android:shortcutDisabledMessage="@string/disabled_message">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example"
            android:targetClass="com.example.Activity" />
        <categories android:name="android.shortcut.category" />
    </shortcut>
</shortcuts>

问题来自 targetClass 变量,因为我找不到启动 Fragment 而不是 Activity 的方法。我想从快捷方式启动的大多数主要页面都是显示在 Activity 中的 Fragments。我怎样才能让 intent 直接启动到 Fragment

最佳答案

您可以执行以下操作:

1) 在shortcuts.xml中指定的intent中,设置自定义intent操作字符串:

 <intent
        android:action="com.your.packagename.custom.name"
        android:targetPackage="com.example"
        android:targetClass="com.example.Activity" />

2) 检查 getIntent().getAction() 以获取 android:targetClass 中指定的 Activity 中的 Intent 操作:

public void onCreate(Bundle savedInstanceState){
    if (CUSTOM_NAME.equals(getIntent().getAction())) {
        // do Fragment transactions here 
    }
}

关于android - 从 Android 7.1 应用程序快捷方式启动 fragment (而不是 Activity ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266221/

相关文章:

java - 如何在 Android Studio 中从没有字符串 Id 的解析 JSON 中获取字符串

android - 使用导航组件为每个 fragment 自定义工具栏

android - 具有动态功能的即时应用程序始终显示带有 1 个选项的消歧对话框

android - 应用程序图标在 Android 设备上不可见

android - 尝试在 customListAdapter 上设置一个 TextView 时,应用程序强制关闭

android - android 手机中的磁力计传感器获取方向

java - 不要在旋转时加载相关的横向/纵向布局

android - 为什么屏幕旋转时 fragment 不保留状态?

android - 基于库的应用程序安装 2 个 APK 文件,而不是一个 - 为什么?

android - 我可以使用 OPENGL 制作逐帧动画吗?