Handling back button in Android Navigation Component

前端 未结 23 1260
遥遥无期
遥遥无期 2020-11-29 18:20

I\'d like to know how properly handle system back button action using Navigation Controller. In my app I have two fragments (for ex. fragment1 and fragment2) and I have an a

相关标签:
23条回答
  • 2020-11-29 18:59

    In 2.1.0-alpha06

    If you want to handle backpress only in current fragment

    requireActivity().onBackPressedDispatcher.addCallback(this@LoginFragment) {
        // handle back event
    }
    

    For whole Activity

    requireActivity().onBackPressedDispatcher.addCallback() {
        // handle back event
    }
    
    0 讨论(0)
  • 2020-11-29 19:00

    Depending on your logic, if you want to close only the current fragment you have to pass viewLifecycleOwner, code is shown below:

       requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                requireActivity().finish()
            }
        })
    

    However, if you want to close application on backPressed no matter from what fragment(probably you wouldn't want that!), don't pass the viewLifecycleOwner. Also if you want to disable the back button, do not do anything inside the handleOnBackPressed(), see below:

     requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                // do nothing it will disable the back button
            }
        })
    
    0 讨论(0)
  • 2020-11-29 19:00

    My Opinion requireActivity().onBackPressed()

    requireActivity().onBackPressed()
    
    0 讨论(0)
  • 2020-11-29 19:01

    For anyone looking for a Kotlin implementation see below.

    Note that the OnBackPressedCallback only seems to work for providing custom back behavior to the built-in software/hardware back button and not the back arrow button/home as up button within the actionbar/toolbar. To also override the behavior for the actionbar/toolbar back button I'm providing the solution that's working for me. If this is a bug or you are aware of a better solution for that case please comment.

    build.gradle

    ...
    implementation "androidx.appcompat:appcompat:1.1.0-rc01"
    implementation "androidx.navigation:navigation-fragment-ktx:2.0.0"
    implementation "androidx.navigation:navigation-ui-ktx:2.0.0"
    ...
    

    MainActivity.kt

    ...
    import androidx.appcompat.app.AppCompatActivity
    ...
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            setContentView(R.layout.activity_main)
    
            ...
    
            val navController = findNavController(R.id.nav_host_fragment)
            val appBarConfiguration = AppBarConfiguration(navController.graph)
    
            // This line is only necessary if using the default action bar.
            setupActionBarWithNavController(navController, appBarConfiguration)
    
            // This remaining block is only necessary if using a Toolbar from your layout.
            val toolbar = findViewById<Toolbar>(R.id.toolbar)
            toolbar.setupWithNavController(navController, appBarConfiguration)
            // This will handle back actions initiated by the the back arrow 
            // at the start of the toolbar.
            toolbar.setNavigationOnClickListener {
                // Handle the back button event and return to override 
                // the default behavior the same way as the OnBackPressedCallback.
                // TODO(reason: handle custom back behavior here if desired.)
    
                // If no custom behavior was handled perform the default action.
                navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
            }
        }
    
        /**
         * If using the default action bar this must be overridden.
         * This will handle back actions initiated by the the back arrow 
         * at the start of the action bar.
         */
        override fun onSupportNavigateUp(): Boolean {
            // Handle the back button event and return true to override 
            // the default behavior the same way as the OnBackPressedCallback.
            // TODO(reason: handle custom back behavior here if desired.)
    
            // If no custom behavior was handled perform the default action.
            val navController = findNavController(R.id.nav_host_fragment)
            return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
        }
    }
    

    MyFragment.kt

    ...
    import androidx.activity.OnBackPressedCallback
    import androidx.fragment.app.Fragment
    ...
    
    class MyFragment : Fragment() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val onBackPressedCallback = object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    // Handle the back button event
                }
            }
            requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback)
        }
    }
    

    The official documentation can be viewed at https://developer.android.com/guide/navigation/navigation-custom-back

    0 讨论(0)
  • 2020-11-29 19:02

    The recommended approach is to add an OnBackPressedCallback to the activity's OnBackPressedDispatcher.

    requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) { 
        // handle back event
    }
    
    0 讨论(0)
  • 2020-11-29 19:04

    Use this if you're using fragment or add it in your button click listener. This works for me.

    requireActivity().onBackPressed()
    

    Called when the activity has detected the user's press of the back key. The getOnBackPressedDispatcher() OnBackPressedDispatcher} will be given chance to handle the back button before the default behavior of android.app.Activity#onBackPressed()} is invoked.

    0 讨论(0)
提交回复
热议问题