1

I have a form and want to get a confirmation message from the user before the user leaves it.

i want provide custom back button when user touch this button:

android navigation back button

i try this:

val onBackPressedCallback = object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
            }
        }
requireActivity().onBackPressedDispatcher.addCallback(this,onBackPressedCallback)

but only seems to work for providing custom back behavior to the built-in software/hardware back button and not the back arrow button

How can I do this?

peyman
  • 143
  • 1
  • 9
  • 1
    in java we do it like this https://stackoverflow.com/questions/36457564/display-back-button-of-action-bar-is-not-going-back-in-android, just convert it to kotlin. – L2_Paver Oct 14 '19 at 01:29

3 Answers3

1

Use onSupportNavigateUp, and replace yourCurrentFragmentID to your current fragment id. All these should be done in MainActivity.

 navController = findNavController(R.id.nav_host_fragment)

 setupActionBarWithNavController(navController!!)

 override fun onSupportNavigateUp(): Boolean {
        return when(navController?.currentDestination?.id) {
            R.id.yourCurrentFragmentID -> {
                showDialog()
                true
            }
            else -> navController?.navigateUp()!!
        }
    }

Edit

If your fragment already use onOptionsItemSelected, you can handle the logic by checking itemId.

 override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == R.id.save) {
           // your save button code logic 
        }else if(id ==android.R.id.home){ 
            // display confirmation message
        }
        return super.onOptionsItemSelected(item)
    }
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • thanks, this is handle back button but What should I do if I want do some operations that require fragment form information? – peyman Oct 14 '19 at 09:22
  • 1
    you mean you want to pass the fragment information to confirmation message? – John Joe Oct 14 '19 at 09:27
  • yes, for example show some data in dialog or run a function that need fragment form data – peyman Oct 14 '19 at 09:34
  • 1
    thanks,this is i want it and mark it as correct answer. can i stop running `navController.navigateUp()` in fragment. i can `navigateUp` with `findNavController().navigateUp()` but how can i stop it? – peyman Oct 14 '19 at 10:00
  • Excuse me, it was a mistake from me. I must to use the code to prevent execution of navigate that you post before :) – peyman Oct 14 '19 at 10:15
  • 1
    You can actually ignore my first answer. Use the second answer is good enough to go. – John Joe Oct 14 '19 at 10:19
  • i mean i must disable `navigateUp` for this fragment in` activity` because when i click on the `back arrow icon` dialog is shown and i go back to the previous fragment while the dialog still there. if i want to stay in current fragment i must disable `navigateUp` or can it be done in fragment? – peyman Oct 14 '19 at 10:34
  • 1
    Make it back to previous fragment if the ok button in dialog is pressed. Otherwise make it stay in current fragment. – John Joe Oct 15 '19 at 02:50
0

Use this code to set Activity on backpress.

override fun onBackPressed() {
        if (isDiscardChanges) {
            discardDialog()
        } else {

            super.onBackPressed()
        }
    }
Chintan
  • 394
  • 2
  • 7
-1

If you only want to go back from Activity, then you can add within AndroidManifest.xml child class Activity.

android:parentActivityName="Parent_Activity_Name 
John Joe
  • 12,412
  • 16
  • 70
  • 135
rachna
  • 124
  • 8