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

edw*_*ard 8 android android-manifest android-fragments 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>
Run Code Online (Sandbox Code Playgroud)

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

Tin*_*ran 24

您可以执行以下操作:

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

 <intent
        android:action="com.your.packagename.custom.name"
        android:targetPackage="com.example"
        android:targetClass="com.example.Activity" />
Run Code Online (Sandbox Code Playgroud)

2)检查getIntent().getAction()在以下指定的活动中的for intent动作android:targetClass:

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