Kotlin close fragment onclick - android

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.

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.

How to clear/reset a map from outside the fragment?

I have a simple app for testing. It has a "basic activity" that Android Studio brings by default. Also a Google maps fragment shown in the basic activity (the code is at the end of the post).
As you all know, the basic activity has a floating button like this:
The main problem is that I'm completely new to working with fragments, and I don't quite understand how they work (I am working on it) but what I want to do is that: by clicking on the floating button, the map is "restarted", reloading again and cleaning it of markers that the user has placed.
What would be the correct way to do this?
I have found several suggestions but I can't get them to work or I can't see how to implement them correctly. One of the ways I have tried is to detach and attach the map fragment, but this causes it to crash and shows no results. The code is the following, which I add in the floating button listener:
val frg : Fragment? = supportFragmentManager.findFragmentById(R.id.map);
val frgTransac = supportFragmentManager.beginTransaction();
if (frg != null) {
frgTransac.detach(frg);
frgTransac.attach(frg);
frgTransac.commit();
}
Another option is to use "googleMap.clear" but I don't know exactly how to get access to that object from the floating button listener.
I hope you can help me with this and, above all, understand how the fragments work and what I am doing wrong.
Main activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
// CLEAR MAP FROM HERE
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
Map Fragment
class FMaps : Fragment() {
private val callback = OnMapReadyCallback { googleMap ->
val sydney = LatLng(-34.0, 151.0)
googleMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_f_maps, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(callback)
}
}
You can use custom interface defined in the fragment and then called from the activity when the button is pressed (example how to create interface here: Communicating between a fragment and an activity - best practices). When you press the button and the interface is called, you can just refresh the map using mapFragment?.getMapAsync(callback) inside the fragment.

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

onKeyDown() event not triggered when in DialogFragment

I capture Key Events (from an external keyboard) within my App. I use onKeyDown() method from Activity. In my app I switch between different Fragments. If I am in a normal Fragment then Activity's onKeyDown() is triggered when pressing buttons. But when I use a DialogFragment as a Dialog then pressing the button does not trigger Activity'sonKeyDown()` any more.
Here some sample code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onClick(view: View) {
// a) Key Event works if adding it via a fragment transaction by my own
// val fragment = MyDialogFragment.newInstance()
// val fragmentTransaction = supportFragmentManager.beginTransaction()
// fragmentTransaction.add(R.id.fr_container, fragment, fragment.javaClass.name)
// fragmentTransaction.commit()
// b) Key Event doesn't work if showing as a dialog
val fragment = MyDialogFragment.newInstance()
fragment.show(supportFragmentManager, fragment.javaClass.name)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
Log.i(javaClass.name, "onKeyDown() keyCode: $keyCode")
return true
}
}
And my two fragments:
class MyNormalFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_my_normal, container, false)
}
}
class MyDialogFragment : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_my_dialog, container, false)
}
companion object {
fun newInstance() = MyDialogFragment()
}
}
As soon as I call a) show() to open the MyDialogFragment then the key events are not captured any more. But if I open MyDialogFragment b) via custom Fragments transaction then the key events are still captured, but my Fragment isn't shown as a Dialog any more.
What do I have to do to let the event also trigger when my dialog is displayed?
Ridcully's answer is right. I just wanted to post what I changed inside MyDialogFragment to keep on capturing key events:
class MyDialogFragment : DialogFragment() {
private val keyEventListener = DialogInterface.OnKeyListener { dialog, keyCode, event ->
Log.i(javaClass.name, "onKey() keyCode: $keyCode")
true
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog.setOnKeyListener(keyEventListener)
return inflater.inflate(R.layout.fragment_my_dialog, container, false)
}
override fun onDestroyView() {
dialog.setOnKeyListener(null)
super.onDestroyView()
}
companion object {
fun newInstance() = MyDialogFragment()
}
}
A Dialog is shown in/as a separate Window, so your Activity doesn't have the focus for keypresses any more. However, the Dialog has it's own onKeyDown method, so you can make use of that.
Just add a simple DialogInterface.OnKeyListener directly on DialogFragment

Base Navigation Fragment for multiple Fragments

Is it possible to create a Navigation Fragment to contain my navigation back button click logic.
Multiple Fragment's that have a back button would then be able then inherit from the Navigation Fragment.
I'm new to Kotlin development. As you see below the SigninFragment inflates the view, I'm not sure how to get a reference to the view & back button in a parent Navigation Fragment
class SigninFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_signin, container, false)
// Navigation back button logic
var headerBackButton = view.findViewById<ImageButton>(R.id.headerBackButton)
headerBackButton.setOnClickListener {
val navController = NavHostFragment.findNavController(this#SignInFragment)
navController.navigateUp()
}
return view
}
}
I'm not sure if I got your problem right but could this be the trick?
open class NavigationFragment() : Fragment() {
fun asignNavigationBackClickListener(backButton: View) {
backButton.setOnClickListener {
val navController = NavHostFragment.findNavController(this#NavigationFragment)
navController.navigateUp()
}
}
}
class SigninFragment : NavigationFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_signin, container, false)
asignNavigationBackClickListener(view.findViewById(R.id.headerBackButton))
return view
}
}
I think you can use this code for going back to the previous activity:
headerBackButton.setOnClickListener {
finish()
}

Categories

Resources