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

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()

Related

How to hide ModalBottomSheetLayout on Back Press / Back Gesture

Im using Compose via ComposeView in a Fragment backed by Graph Navigation.
Im using ModalBottomSheetLayout and need to hide it on back Press.
I have tried using BackHandler, but it is not working.
class fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
BackHandler(true) {
Log.i("compose_check","back Pressed") // not getting triggered
}
}
}
}
}
I have Overrided onBackPressed() in the Activity. After removing that, it worked fine.

Kotlin close fragment onclick

I have a fragment that is called from the activity_main screen when a main menu drop down option is clicked. I am trying to have the fragment close and show the activity_main screen again when the cancel button is clicked. I have user input in the activity_main screen that I would like to still be there if the fragment is closed while clicking cancel but at this point i will settle for the fragment just closing and the activity_main showing back at it's original state.
I am using androidx and all of the answers I have found so far have been compatible with android.support.v7
This is the code that i have in my fragment, it does not do anything at all when I click the 'Cancel' button, the fragment just continues to sit there over the activity_main.
class SaveFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.savefragment, container, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
b_cancel.setOnClickListener {
fragmentManager?.popBackStack()
}
}
}
Here is the code that calls the savefragment from the MainActivity:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.i_save -> {
val SaveFragment = SaveFragment()
supportFragmentManager.beginTransaction().replace(R.id.activity_main_main, SaveFragment).commit()
true
}
R.id.i_recall -> {
val RecallFragment = RecallFragment()
supportFragmentManager.beginTransaction().replace(R.id.activity_main_main, RecallFragment).commit()
true
}
else -> super.onOptionsItemSelected(item)
}
}
Any help at all in Kotlin would be appreciated.

Conditional Up and Back Actions with Android Navigation Component Library

How to provide conditional Up and Back navigation when using Android's Navigation Component library?
For example, my app has a contact book. When creating a new contact, if the user presses back before filling out any info, I'd like to go back to the list of contacts. If the user filled out info, I'd like to go to the detail view of that contact.
In your fragment's onCreate, add an OnBackPressedCallback callback:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requireActivity().onBackPressedDispatcher.addCallback(this) {
backOrUpPressed()
}
}
Then, after you setup your toolbar with the NavController, set a NavigationOnClickListener:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Setup the view
setSupportActionBar(toolbar)
toolbar.setupWithNavController(findNavController(), AppBarConfiguration(findNavController().graph))
toolbar.setNavigationOnClickListener { backOrUpPressed() }
}
Then implement your custom Up/Back logic in backorUpPressed()

how to make custom back button to go back to certain destination using navigation controller in Android?

I need to make a custom behaviour, when the user press the back button then the user will go to certain destination programatically. I actually have read this Handling back button in Android Navigation Component
but I don't understand how to use that custom back button code.it seems weird to me.
I have tried using this code below
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentView = inflater.inflate(R.layout.fragment_search_setting, container, false)
// set custom back button
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
// navigate to certain destination
Navigation.findNavController(fragmentView).popBackStack(R.id.destination_create_event, false)
}
return fragmentView
}
but I get type mismatch error like this
You must create new Instance of the OnBackPressedCallback abstract class and implement its abstract method .
I hope this helps you:
val callback = requireActivity().onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true){
override fun handleOnBackPressed() {
Navigation.findNavController(fragmentView).popBackStack(R.id.destination_create_event, false)
}
})
// The callback can be enabled or disabled here or in the lambda
}

How to prevent dialog to appear when I go to previous fragment after dialog has been dismissed?

So right now, when I go to Fragment B from Fragment A, and show a dialog in the onCreate() of Fragment B, and then dismissed it (by clicking on the close button of the dialog) and go back to previous fragment(Fragment A) the dialog appears again for some reason(in Fragment A). If I repeat the action for example 5 times and go back each of those 5 times (to the previous fragment[Fragment A]) the dialog appears 5 times in a row in the previous fragment(Fragment A) . For some reason he is recording the dialogs that are being shown.
So my dialog code is this:
fun Fragment.showDialog(fragment: DialogFragment, tag: String) {
val ft = fragmentManager?.beginTransaction()
val dialog = fragmentManager?.findFragmentByTag(tag) as? DialogFragment
dialog?.let {
ft?.remove(it)
}
dialog.
ft?.addToBackStack(null)
fragment.show(ft, tag)
}
My call to the dialog in the fragment is this:
showDialog(SuccessDialog.newInstance(), SuccessDialog.TAG)
My success dialog fragment is this:
class SuccessDialog : DialogFragment() {
companion object {
const val TAG = "SUCESS_DIALOG"
fun newInstance() = SuccessDialog()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View =
inflater.inflate(R.layout.dialog_success, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
dialog?.window?.setDimAmount(0.8f)
closeButton.setOnClickListener {
dialog.cancel()
dialog.dismiss()
}
}
}
This is my activity main back press body:
supportFragmentManager.popBackStack()

Categories

Resources