Ask*_*ani 4 android android-fragments bottomnavigationview android-architecture-components android-jetpack
我正在使用navigation component
和BottomNavigationView
,我面临一个问题,那就是当我从fragment
1>2>5>4>3转到时,当我按下后退按钮时,我得到fragment
1。我知道这是默认行为,但我不想要这个,我想保存它们,backstack
所以当我按下后退按钮时,它应该转到fragment
4 而不是 1。我一直在尝试和搜索,但找不到任何解决方案。我可以把fragment
手动进backstack
的kotlin
?
我的代码:
活动_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
.......................
.......................>
<androidx.appcompat.widget.Toolbar
................
................/>
<fragment
android:id="@+id/app_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="@navigation/app_nav"
..............
............../>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/app_bottom_nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/white_grey_border_bottom"
app:menu="@menu/bottom_nav_menu"
app:labelVisibilityMode="unlabeled"
...........
.........../>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
app_nav.xml
<navigation
...........
...........
android:id="@+id/app_nav"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.instagram_clone.ui.HomeFragment"
android:label="HomeFragment"
tools:layout="@layout/fragment_home"/>
.............
.............
.............
.............
</navigation>
Run Code Online (Sandbox Code Playgroud)
主活动.kt
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
val bottomNavView = binding.appBottomNavView
val navController = findNavController(R.id.app_nav_host_fragment)
bottomNavView.setupWithNavController(navController)
}
}
Run Code Online (Sandbox Code Playgroud)
ian*_*ake 13
根据onNavDestinationSelected()上的文档(setupWithNavController()
选择 MenuItem 时使用的方法):
默认情况下,返回堆栈将弹出回到导航图的起始目的地。具有的菜单项
android:menuCategory="secondary"
不会弹出返回堆栈。
因此,只需添加android:menuCategory="secondary"
到与您的BottomNavigationView
.
归档时间: |
|
查看次数: |
4723 次 |
最近记录: |