简体   繁体   中英

android - Hide or close BottomSheetDialog on item menu click (Kotlin)

Im currently working on an app, which has a BottomSheetDialog as navigation menu. This menu, is called by the Toolbar Navigation Item.

When calling the BottomSheetDialog, it shows up, and clicking on an item of the list creates the related activity, which is expected. However, when i close the newly opened activity the BottomSheetDialog shows up again, which is not the intended behaviour.

它能做什么

Are there any ways to prevent the BottomSheetFragment to show up?

I tried using .also after the creation of the Intent, but there are no function dedicated to hide or close the Dialog

Here is the BottomSheetFragment code:

class frgBottomSheetDrawer : BottomSheetDialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_bottomsheet, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        navDrawer.setNavigationItemSelectedListener { menuItem ->
            when (menuItem!!.itemId) {
                R.id.ndListFolder -> this.startActivity(Intent(activity, ndActFolder::class.java))
                R.id.ndListSettings -> this.startActivity(Intent(activity, ndActSettings::class.java))
                R.id.ndListAbout -> this.startActivity(Intent(activity, actAbout::class.java))
            }
            true
        }
    }
}

To close the bottomSheet Dialog after an Item Click, try the following

 navDrawer.setNavigationItemSelectedListener { menuItem ->
            when (menuItem!!.itemId) {
                R.id.ndListFolder -> this.startActivity(Intent(activity, 
                ndActFolder::class.java))
                 dismiss() // add this whenever you want to close the bottomSheet

            }
            true
        }

So you simply need to add this method dismiss()

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