如何以编程方式更改导航图的起始目的地 [Jetpack] [英] How to change start destination of a navigation graph programmatically [Jetpack]
问题描述
基本上,我有以下导航图:
我想在到达它后立即将导航图中的起点更改为 fragment 2
(以防止在按下后退按钮时返回到 fragment 1
- 例如与启动画面).
这是我的代码:
navGraph = navController.getGraph();navGraph.setStartDestination(R.id.fragment2);navController.setGraph(navGraph);
但是,显然它不起作用,按下后退按钮后它返回到fragment 1
.
我做错了吗?还有其他解决办法吗?
更新:
当你有这样的导航图时:
<片段android:id="@+id/firstFragment"android:name="com.appname.package.FirstFragment" ><动作android:id="@+id/action_firstFragment_to_secondFragment"app:destination="@id/secondFragment"/></片段><片段android:id="@+id/secondFragment"android:name="com.appname.package.SecondFragment"/>
并且您想导航到第二个片段并使其成为图形的根,指定下一个 NavOptions
:
NavOptions navOptions = new NavOptions.Builder().setPopUpTo(R.id.firstFragment, true).建造();
并将它们用于导航:
Navigation.findNavController(view).navigate(R.id.action_firstFragment_to_secondFragment, bundle, navOptions);
<块引用>
setPopUpTo(int destinationId, boolean inclusive)
- 在导航前弹出给定的目的地.这将从返回堆栈中弹出所有不匹配的目的地,直到找到该目的地.
destinationId
- 要弹出的目的地,清除所有中间目的地.
inclusive
- true 也从返回堆栈中弹出给定的目的地.
替代方案:
<片段android:id="@+id/firstFragment"android:name="com.appname.package.FirstFragment" ><动作android:id="@+id/action_firstFragment_to_secondFragment"应用程序:目的地=@id/secondFragment"app:popUpTo="@+id/firstFragment"app:popUpToInclusive="true"/></片段><片段android:id="@+id/secondFragment"android:name="com.appname.package.SecondFragment"/>
然后在你的代码上:
findNavController(fragment).navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment())
旧答案
已弃用:NavOptions
中用于操作和关联 API 的 clearTask
属性已被弃用.>
来源:https://developer.android.com/jetpack/docs/release-笔记
如果您想将根片段更改为 fragment 2
(例如,在按 fragment 2
上的后退按钮后,您将退出应用程序),您应该放置下一个属性到您的 action
或 destination
:
app:clearTask="true"
实际上它的外观如下:
<片段android:id="@+id/firstFragment"android:name="com.appname.package.FirstFragment"android:label="fragment_first" ><动作android:id="@+id/action_firstFragment_to_secondFragment"应用程序:目的地=@id/secondFragment"应用程序:clearTask="true"/></片段><片段android:id="@+id/secondFragment"android:name="com.appname.package.SecondFragment"android:label="fragment_second"/>
我已将 app:clearTask="true"
添加到操作中.
现在,当您执行从 fragment 1
到 fragment 2
的导航时,请使用下一个代码:
Navigation.findNavController(view).navigate(R.id.action_firstFragment_to_secondFragment);
Basically, I have the following navigation graph:
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).
This is my code:
navGraph = navController.getGraph();
navGraph.setStartDestination(R.id.fragment2);
navController.setGraph(navGraph);
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"/>
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();
And use them for the navigation:
Navigation.findNavController(view).navigate(R.id.action_firstFragment_to_secondFragment, bundle, navOptions);
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
- The destination to pop up to, clearing all intervening destinations.
inclusive
- true to also pop the given destination from the back stack.
ALTERNATIVE:
<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"/>
And then on your code:
findNavController(fragment).navigate(
FirstFragmentDirections.actionFirstFragmentToSecondFragment())
Old answer
Deprecated: The clearTask
attribute for actions and the associated API in NavOptions
has been deprecated.
Source: https://developer.android.com/jetpack/docs/release-notes
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"/>
I've added app:clearTask="true"
to action.
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屋!