从Android 7.1应用程序快捷方式启动Fragment(而不是Activity) [英] Launch Fragment (instead of Activity) from Android 7.1 App Shortcut

查看:223
本文介绍了从Android 7.1应用程序快捷方式启动Fragment(而不是Activity)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have decided to look at adding static shortcuts into an application, using this page as reference:

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

XML当前我的快捷方式如下:

The XML for my shortcuts currently looks like so:

<?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 的方法。我想从快捷方式启动的大多数主页是活动中显示的片段。如何获得意向直接启动为片段的意图?

The issue comes from the targetClass variable as I cannot find a way to launch a Fragment rather than an Activity. Most of the main pages I would want to launch from the shortcuts are Fragments displayed within an Activity. How can I get the intent to launch straight to a Fragment instead?

推荐答案

您可以执行以下操作:

1)在快捷方式中指定的意图中.xml ,设置自定义的Intent操作字符串:

1) In the intent specified in shortcuts.xml, set a custom intent action string:

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

2)检查 getIntent()。getAction()用于 android:targetClass 中的意图动作:

2) Check for the getIntent().getAction() for intent action in the Activity specified in android:targetClass:

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

这篇关于从Android 7.1应用程序快捷方式启动Fragment(而不是Activity)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆