我正在嘗試更新我的應用程式以使用BottomNavigationView. 第一個選項卡包含一個HostFragment帶有加載微調器的加載微調器,它執行網路請求以確定應在該選項卡中顯示哪個片段,或者HomeFragment或LockedFragment。
MainActivity處理 的初始設定BottomNavigationView:
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment = supportFragmentManager.findFragmentById(
R.id.nav_host_container
) as NavHostFragment
navController = navHostFragment.navController
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
bottomNavigationView.setupWithNavController(navController)
appBarConfiguration = AppBarConfiguration(
setOf(R.id.mainFragment)
)
setupActionBarWithNavController(navController, appBarConfiguration)
}
我的主導航圖如下所示:
<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/nav_graph"
app:startDestination="@ id/home">
<include app:graph="@navigation/home"/>
<include app:graph="@navigation/list"/>
<include app:graph="@navigation/form"/>
</navigation>
主圖看起來像:
<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/home"
app:startDestination="@ id/hostFragment">
<fragment
android:id="@ id/hostFragment"
android:name="com.example.android.bottomnav.homescreen.HostFragment"
android:label="Host">
<action
android:id="@ id/action_hostFragment_to_homeFragment"
app:destination="@id/homeFragment" />
<action
android:id="@ id/action_hostFragment_to_lockedFragment"
app:destination="@id/lockedFragment" />
</fragment>
<fragment
android:id="@ id/homeFragment"
android:name="com.example.android.bottomnav.homescreen.HomeFragment"
android:label="Home" />
<fragment
android:id="@ id/lockedFragment"
android:name="com.example.android.bottomnav.homescreen.LockedFragment"
android:label="Locked"/>
</navigation>
HostFragment get 顯示正常并加載它的資料:
class HostFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_host, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
determineFragmentToShow()
}
private fun determineFragmentToShow() {
lifecycleScope.launchWhenStarted {
// mock network call to determine tab
delay(1500)
// show HomeFragment for the sake of the example, but note that
// this would be dependent on the network call's result above
findNavController().navigate(R.id.action_hostFragment_to_homeFragment)
}
}
}
它成功地將我們導航到HomeFragment.
現在的問題是,每當我按下后退按鈕時,HomeFragment它都會回傳HostFragment而不是關閉應用程式。您可以在此處查看此視頻中的行為。
我試圖在里面設定popUpTo和popUpInclusive標簽是home.xml這樣的:
<fragment
android:id="@ id/hostFragment"
android:name="com.example.android.bottomnav.homescreen.HostFragment"
android:label="Host">
<action
android:id="@ id/action_hostFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:popUpTo="@id/home"
app:popUpToInclusive="true"/>
<action
android:id="@ id/action_hostFragment_to_lockedFragment"
app:destination="@id/lockedFragment"
app:popUpTo="@id/home"
app:popUpToInclusive="true"/>
</fragment>
當從 按下回傳時,應用程式會關閉HomeFragment,但現在每次我切換到一個新選項卡時,它都會創建一個片段的新實體并將其添加到后臺堆疊中。然后按后退按鈕將向后遍歷它們。您可以在此處的視頻中看到這種行為。
那么如何更新嵌套導航圖的起始目的地?
我正在使用最新2.4.0-alpha10的導航組件,以便我可以獲得對多個后臺堆疊的本機支持。任何幫助是極大的贊賞!
uj5u.com熱心網友回復:
我能夠利用這里的答案來獲得適合我的解決方案。
在determineFragmentToShow()in里面HostFragment,我只是findNavController().navigate(R.id.action_hostFragment_to_homeFragment)用
val navController = findNavController()
val graph = navController.graph
val walletGraph = graph.findNode(R.id.home) as NavGraph
walletGraph.setStartDestination(R.id.homeFragment)
navController.navigate(R.id.action_hostFragment_to_homeFragment)
我仍然需要在此處包含popUpTo和popUpInclusive標簽
<fragment
android:id="@ id/hostFragment"
android:name="com.example.android.bottomnav.homescreen.HostFragment"
android:label="Host">
<action
android:id="@ id/action_hostFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:popUpTo="@id/home"
app:popUpToInclusive="true"/>
<action
android:id="@ id/action_hostFragment_to_lockedFragment"
app:destination="@id/lockedFragment"
app:popUpTo="@id/home"
app:popUpToInclusive="true"/>
</fragment>
但這讓我得到了我正在尋找的背部行為!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/324307.html
