java通过快速应用程序快捷方式打开片段
您好,我知道这是一个重复的问题,但我无法从其他线程中找到解决方案
这是我的问题, 我有一个应用程序的底部导航栏与五个片段。我想通过应用程序快捷方式打开这些片段
这里是我创造的
捷径。xml
<shortcuts xmlns:tools="http://schemas.安卓.com/tools"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
<shortcut
安卓:shortcutId="first"
安卓:enabled="true"
安卓:icon="@drawable/shortcut_1"
安卓:shortcutShortLabel="@string/shortcut_short_label_one"
安卓:shortcutLongLabel="@string/shortcut_long_label_one"
tools:targetApi="n_mr1">
<intent
安卓:action="com.xxxxxxx.xxxx.FIRST"
安卓:targetPackage="com.xxxxxxx.xxxx.test"
安卓:targetClass="com.xxxxxxx.xxxx.test.MainActivity" />
</shortcut>
<!-- Specify more shortcuts here. -->
<shortcut
安卓:shortcutId="second"
安卓:enabled="true"
安卓:icon="@drawable/shortcut_2"
安卓:shortcutShortLabel="@string/shortcut_short_label_two"
安卓:shortcutLongLabel="@string/shortcut_long_label_two"
tools:targetApi="n_mr1">
<intent
安卓:action="com.xxxxxxx.SECOND"
安卓:targetPackage="com.xxxxxxx.xxxx.test"
安卓:targetClass="com.xxxxxxx.xxxx.test.SecondFragment" />
</shortcut>
</shortcut>
以下是main活动中的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();
安卓.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();
安卓.support.v4.app.FragmentTransaction fragmentTransactionOne = getSupportFragmentManager().beginTransaction();
fragmentTransactionOne.replace(R.id.content_main, firstFragment, "Fragment one");
fragmentTransactionOne.commit();
}
在这里,第一个activity启动fine,即MainActivity()
。但是点击第二个快捷方式,什么也不会发生
有人解释我做错了什么吗
# 1 楼答案
我有和你一样的问题,我发现了如何移动到碎片。你应该采取行动。把这个放在onCreate里,也许这会对你有帮助
我正在使用kotlin for android
# 2 楼答案
我发现有两件事情看起来不对劲:
在快捷方式上。xml:
这两个快捷方式都应该以“MainActivity”类为目标,因为它是入口点,负责将片段添加到ui中。因此,您需要更改第二个快捷方式上的targetClass,并改为以“MainActivity”为目标
关于onCreate方法:
当该操作对应于第二个快捷方式时,它会在屏幕上设置第二个片段,但该方法会继续并在其上设置第一个片段
我想它应该是一个if-else: