如何以编程方式更改导航图的起始目标[Jetpack] [英] How to change start destination of a navigation graph programmatically [Jetpack]
问题描述
基本上,我有以下导航图:
Basically, I have the following navigation graph:
我想在到达导航图后立即将导航图的起点更改为fragment 2
(以防止在按后退"按钮时返回到fragment 1
-与初始屏幕一样).
I want to change my starting point in navigation graph to fragment 2
right after reaching it (in order to prevent going back to fragment 1
when pressing back button - like with the splash screen).
这是我的代码:
navGraph = navController.getGraph();
navGraph.setStartDestination(R.id.fragment2);
navController.setGraph(navGraph);
但是,很明显,它不起作用,并且按下返回按钮后,它又回到了fragment 1
.
But, obviously it's not working and it gets back to fragment 1
after pressing back button.
我做错了吗? 还有其他解决方案吗?
Am I doing it wrong? Is there any other solution?
推荐答案
更新:
当您具有如下导航图时:
UPDATE:
When you have nav graph like this:
<fragment
android:id="@+id/firstFragment"
android:name="com.appname.package.FirstFragment" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.appname.package.SecondFragment"/>
然后您要导航到第二个片段并将其设为图形的根,请指定下一个NavOptions
:
And you want to navigate to the second fragment and make it root of your graph, specify the next NavOptions
:
NavOptions navOptions = new NavOptions.Builder()
.setPopUpTo(R.id.firstFragment, true)
.build();
并使用它们进行导航:
Navigation.findNavController(view).navigate(R.id.action_firstFragment_to_secondFragment, bundle, navOptions);
setPopUpTo(int destinationId, boolean inclusive)
-在导航之前弹出到给定的目的地.这将从后堆栈中弹出所有不匹配的目标,直到找到该目标为止.
setPopUpTo(int destinationId, boolean inclusive)
- Pop up to a given destination before navigating. This pops all non-matching destinations from the back stack until this destination is found.
destinationId
-弹出的目标,清除所有中间目标.
destinationId
- The destination to pop up to, clearing all intervening destinations.
inclusive
-true也可以从后堆栈中弹出给定的目的地.
inclusive
- true to also pop the given destination from the back stack.
<fragment
android:id="@+id/firstFragment"
android:name="com.appname.package.FirstFragment" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:popUpTo="@+id/firstFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.appname.package.SecondFragment"/>
然后在您的代码上:
findNavController(fragment).navigate(
FirstFragmentDirections.actionFirstFragmentToSecondFragment())
旧答案
已弃用: NavOptions
中的动作和关联API的clearTask
属性已被弃用.
Old answer
Deprecated: The clearTask
attribute for actions and the associated API in NavOptions
has been deprecated.
来源: https://developer.android.com/jetpack/docs/release-笔记
如果要将根片段更改为fragment 2
(例如,按下fragment 2
上的后退按钮,您将退出应用程序),则应将下一个属性放入action
或destination
: >
If you want to change your root fragment to fragment 2
(e.g. after pressing back button on fragment 2
you will exit the app), you should put the next attribute to your action
or destination
:
app:clearTask="true"
实际上,它看起来是另一种方式:
Practically it looks in a next way:
<fragment
android:id="@+id/firstFragment"
android:name="com.appname.package.FirstFragment"
android:label="fragment_first" >
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:clearTask="true" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.appname.package.SecondFragment"
android:label="fragment_second"/>
我已将app:clearTask="true"
添加到动作中.
I've added app:clearTask="true"
to action.
现在,当您执行从fragment 1
到fragment 2
的导航时,请使用以下代码:
Now when you perform navigation from fragment 1
to fragment 2
use the next code:
Navigation.findNavController(view)
.navigate(R.id.action_firstFragment_to_secondFragment);
这篇关于如何以编程方式更改导航图的起始目标[Jetpack]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!