Android Jet Pack导航,setupWithNavController()重新创建片段
使用jetpack的底部导航栏时,我的导航视图有问题 下面是我的流程的工作原理 我有4个视图,每个视图都有重定向,比如当我在最后一个导航栏选择时,我有一个Android Jet Pack导航,setupWithNavController()重新创建片段,android,android-fragments,kotlin,navigation,fragment,Android,Android Fragments,Kotlin,Navigation,Fragment,使用jetpack的底部导航栏时,我的导航视图有问题 下面是我的流程的工作原理 我有4个视图,每个视图都有重定向,比如当我在最后一个导航栏选择时,我有一个片段a->片段B,当我返回到第一个导航栏选择时,当我返回到第四个导航栏选择时,它又是片段a。我相信这是因为片段正在使用setupWithNavController()重新创建,如果是这样,jetpack是否有解决方法 这是我的代码,供参考 <navigation xmlns:android="http://schemas.android.
片段a
->片段B
,当我返回到第一个导航栏选择时,当我返回到第四个导航栏选择时,它又是片段a
。我相信这是因为片段正在使用setupWithNavController()
重新创建,如果是这样,jetpack是否有解决方法
这是我的代码,供参考
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@+id/splashFragment">
<fragment
android:id="@+id/selectionFragment"
android:name="whitecloak.com.allibuy.app.selection.SelectionFragment"
android:label="fragment_selection"
tools:layout="@layout/fragment_selection" >
<action
android:id="@+id/toLogin"
app:destination="@id/loginFragment"
app:launchSingleTop="true"
app:popUpTo="@+id/nav_graph"
app:popUpToInclusive="true/>
</fragment>
<fragment
android:id="@+id/splashFragment"
android:name="whitecloak.com.allibuy.app.splash.SplashFragment"
android:label="fragment_splash"
tools:layout="@layout/splash_fragment"
>
<action
android:id="@+id/toMain"
app:destination="@id/mainFragment"
app:launchSingleTop="true"
app:popUpTo="@+id/nav_graph"
app:popUpToInclusive="true"/>
</fragment>
我刚刚包括了第一个和最后一个选项卡的XML。非常感谢你
编辑
它与底部导航的设置无关。这相当于Android实现中的显式行为。我将引用并解释: 行为 在Android上:应用程序导航到目的地的顶级屏幕。任何先前的用户交互和临时屏幕状态都会重置,例如滚动位置、选项卡选择和在线搜索 从 这意味着,当您单击BottomNav上的项目时,它应该始终返回到该流堆栈上的第一个片段
如果我不清楚,这里有一个伪表示: 底部导航项目1>片段1a>片段1b 底部导航项目2>片段2a>片段2b
如果点击
BottomNavItem#1
,它将加载Fragment1A
。然后想象一下,使用一个按钮,它会显示Fragment1B
。如果现在单击BottomNavItem#2
,您将看到Fragment2A
。现在,如果你点击BottomNavItem#1
,它将显示Fragment1A
(不是你上次看到的Fragment1B
),因为它是堆栈/流的根。我认为你应该加入一些Kotlin来帮助我们理解上下文和正在完成的代码。一、 例如,希望查看调用的是bottomNav.setupWithNavController
。@shkschneider-我更新了代码,代码就在那里。非常感谢。你能推荐我做些什么来解决问题,或者我会做些改变吗?对于标准的小部件,我肯定是非常不鼓励的,而且我从来没有实现过这样的事情(小心“后退”行为),所以很遗憾否,我不知道如何定制它。我只是解释了发生的原因。祝你好运。非常感谢你带给我这个信息,先生!不客气。如果你发现有用的东西,回答你自己的问题,它可能会帮助别人;)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/splashFragment"
android:icon="@drawable/home"
android:title="@string/home"
app:popUpTo="@id/nav_graph" />
<item
android:id="@+id/tabCart"
android:icon="@drawable/cart"
android:title="@string/cart"
app:popUpTo="@id/nav_graph" />
<item
android:id="@+id/tabNotif"
android:icon="@drawable/notification"
android:title="@string/notification"
app:popUpTo="@id/nav_graph"/>
<item
android:id="@+id/selectionFragment"
android:icon="@drawable/user"
android:title="@string/account"
app:popUpTo="@id/nav_graph" />
bottomNav.setupWithNavController(findNavController(R.id.nav_main))
class MainNavigation : DaggerAppCompatActivity() {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var viewModel: MainNavigationViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this, viewModelFactory)[MainNavigationViewModel::class.java]
bottomNav.setupWithNavController(findNavController(R.id.nav_main))
}
}