首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过快速应用快捷方式打开片段

通过快速应用快捷方式打开片段
EN

Stack Overflow用户
提问于 2018-06-10 03:56:11
回答 1查看 698关注 0票数 3

您好,我知道这是一个重复的问题,但我无法从其他线程找到解决方案。

这就是我的问题,我有一个底部导航栏有五个片段的应用程序。我想通过应用程序快捷方式打开这些片段。

这是我所创造的,

shortcut.xml

<shortcuts xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
    android:shortcutId="first"
    android:enabled="true"
    android:icon="@drawable/shortcut_1"
    android:shortcutShortLabel="@string/shortcut_short_label_one"
    android:shortcutLongLabel="@string/shortcut_long_label_one"
    tools:targetApi="n_mr1">
    <intent
        android:action="com.xxxxxxx.xxxx.FIRST"
        android:targetPackage="com.xxxxxxx.xxxx.test"
        android:targetClass="com.xxxxxxx.xxxx.test.MainActivity" />
</shortcut>
<!-- Specify more shortcuts here. -->
<shortcut
    android:shortcutId="second"
    android:enabled="true"
    android:icon="@drawable/shortcut_2"
    android:shortcutShortLabel="@string/shortcut_short_label_two"
    android:shortcutLongLabel="@string/shortcut_long_label_two"
    tools:targetApi="n_mr1">
    <intent
        android:action="com.xxxxxxx.SECOND"
        android:targetPackage="com.xxxxxxx.xxxx.test"
        android:targetClass="com.xxxxxxx.xxxx.test.SecondFragment" />
</shortcut>
</shortcut> 

下面是来自MainActivity的onCreate()

private static final String Second = "com.xxxxxxx.xxxx.SECOND";

    //code vomited  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Second.equals(getIntent().getAction())){
            SecondFragment secondFragment = new SecondFragment();
            android.support.v4.app.FragmentTransaction fragmentTransactionTwo = getSupportFragmentManager().beginTransaction();
            fragmentTransactionTwo.replace(R.id.content_main, secondFragment, "Fragment two");
            fragmentTransactionTwo.commit();
        }

        BottomNavigationView navigation = findViewById(R.id.navigation);
        BottomNavigationViewHelper.disableShiftMode(navigation);     //disabling the shitty shifting mode
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        FirstFragment firstFragment = new FirstFragment();
        android.support.v4.app.FragmentTransaction fragmentTransactionOne = getSupportFragmentManager().beginTransaction();
        fragmentTransactionOne.replace(R.id.content_main, firstFragment, "Fragment one");
        fragmentTransactionOne.commit();
    }

在这里,第一个活动启动fine,即MainActivity()。但是点击第二个快捷键,什么也不会发生。

有人告诉我我做错了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2020-07-02 14:52:46

我和你有同样的问题,我找到了如何移动到碎片。你应该开始行动了。把这个放到onCreate里,也许这会对你有帮助

if ("com.xxxxxxx.YourShortcutAction".equals(getIntent().getAction())) {
        val fragment = YourFragment()
        addFragment(fragment)
    }

我正在使用kotlin for android

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50778003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档