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

10

我决定研究将静态快捷方式添加到应用程序中,使用这个页面作为参考:

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
启动直接到一个“Fragment”是什么意思?“Fragment”必须附加到一个“Activity”吗? - Tin Tran
@TinTran 我知道,但我想从那个Activity启动那个Fragment。目前我甚至找不到启动一个Fragment子级的方法。 - edwoollard
3个回答

24

你可以采取以下步骤:

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

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

2) 检查在android:targetClass指定的 Activity 中,intent 的操作是否为 getIntent().getAction()

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

CUSTOM_NAME?这个常量包含什么内容? - Morozov
1
在这个例子中,它是 com.your.packagename.custom.name - Tin Tran
还有一个问题,当我尝试进行Fragment事务时,我的片段的第二种类型是错误的。需要'android.app.Fragment'。getFragmentManager().beginTransaction().replace(R.id.fragment_container, new LanguagesSelectorFragment()).commit();. - Morozov
1
你可以将 getFragmentManager() 替换为 getSupportFragmentManager() - Tin Tran
我在我的目标Java类中使用了以下代码:<intent android:action="android.intent.action.VIEW" android:targetPackage="com.soap.mart" android:targetClass="com.soap.mart.MainSliderActivity"> <extra android:name="screen" android:value="true" /></intent>,但是我用//Log.e("***----**", getIntent().getExtras().getString("screen"))来接收值,但是什么也没有发生。 - Akash kumar
显示剩余3条评论

4
你可以使用Shortbread库来轻松实现此目的。
class MainActivity : AppCompatActivity() {
        
    @Shortcut(id = "movies", shortLabel = "Movies", icon = R.drawable.ic_shortcut_movies)
    fun showMovies() {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, MoviesFragment())
            .commit()
    }
}

我不建议使用它,因为它已经有两年以上没有更新了。 - Bartek Pacia
1
1.1.0版本在几天前发布。 - Matthias Robbers

1

您还可以使用DeepLink导航在导航组件上进行重定向,快捷方式XML意图如下所示:

<intent
        android:action="android.intent.action.VIEW"
        android:data="<Your deeplink URI, like appname://feature-to-be-names>"
        android:targetClass="activity class with package name"
        android:targetPackage="application id"/>

在活动中的onCreate()方法中处理深度链接导航

intent.dataString?.let {
        navHostFragment.findNavController().navigate(Uri.parse(it), false)
    }

这将处理多个快捷方式和重定向。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接