简体   繁体   中英

How to hide the icons and only show the back button in the toolbar using navigation component?

I have a single activity app with multiple fragments and by using the navigation components I can navigate between them. On my Activity, I have a toolbar with a menu and nav controller. The toolbar icons are shown in every fragment in my app and in one of the fragments, I want to show only the back button without the rest of the icons. How do I possibly do that?.

Thanks in advance..

You can use java or kotlin I understand them both

My Main Activity

 private lateinit var navController : NavController

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
     navController = navHostFragment.navController
    val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
    bottomNavigationView.setupWithNavController(navController)


    val my_toolbar = findViewById<Toolbar>(R.id.myToolBar)
    setSupportActionBar(my_toolbar)
    setupActionBarWithNavController(navController)
}
 override fun onSupportNavigateUp(): Boolean {
    return navController.navigateUp() || super.onSupportNavigateUp()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu2, menu)
    return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId){
        R.id.filter_list -> Toast.makeText(this, "Filter Was Clicked", Toast.LENGTH_SHORT).show()

        R.id.female_filtering -> Toast.makeText(this, "Female Filter", Toast.LENGTH_SHORT).show()

        R.id.male_filtering -> Toast.makeText(this, "male Filter", Toast.LENGTH_SHORT).show()
    }
    val navController = findNavController(R.id.fragmentContainerView)
    return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}

my menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
    android:id="@+id/favouriteFragment"
    android:title="المفضلة"
    android:icon="@drawable/ic_favorite"
    android:menuCategory="secondary"
    app:showAsAction="always"/>
<item
    android:id="@+id/contactUsFragment"
    android:title="للتواصل"
    android:icon="@drawable/ic_contact"
    android:menuCategory="secondary"
    app:showAsAction="always"/>
<item
    android:id="@+id/filter_list"
    android:title="فلترة"
    android:icon="@drawable/ic_filter"
    android:menuCategory="container"
    app:showAsAction="always">

    <menu>
        <item android:id="@+id/female_filtering"
            android:title="وظائف نسائية"
            app:showAsAction="never"/>
        <item android:id="@+id/male_filtering"
            android:title="وظائف للرجال"
            app:showAsAction="never"/>
    </menu>
</item>

-- I don't have any code on my fragments regarding the toolbar

My main fragment

在此处输入图像描述

I only want to show the back button in this fragment

在此处输入图像描述

please refer https://stackoverflow.com/a/70527808/15529296 this link. It might be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM