These days, I am preparing to write an article about fragment returning stack, but I found that I must introduce OnBackPressedDispatcher first, so this is an article about what. Those who like first-hand information can refer to the official document

series

Back to Jetpack: Dependencies and transitive relationships of Jetpack’s major components

Changes to using activities and fragments under AdroidX

Can you really use a Fragment? Fragments FAQ and new postures for using fragments on androidx

AndroidX Fragment1.2.2 Source code analysis

When

OnBackPressedDispatcher was added in AndroidX Activity 1.0.0 to handle the return logic. Not only do you get a convenient way to handle the return key outside of your Activity. Depending on your needs, you can define OnBackPressedCallback anywhere, make it reusable, or do anything based on the architecture of your application. You no longer need to override the onBackPressed method in your Activity, nor do you need to provide your own abstract code to implement the requirements.

What

ComponentActivity is a base class for FragmentActivity and AppCompatActivity, It allows you to through the use of its OnBackPressedDispatcher (by calling the getOnBackPressedDispatcher ()) to control the behavior of the back button.

OnBackPressedDispatcher controls how the return button event is assigned to one or more OnBackPressedCallback objects. The constructor for OnBackPressedCallback uses a Boolean value for the initial enabled state. Only when the callback isEnabled(that is, isEnabled() returns true) does the scheduler call the callback’s handleOnBackPressed() to handle the return button event. You can change the enabling status by calling setEnabled().

Callbacks are added through the addCallback method. The addCallback() method in LifecycleOwner is highly recommended. This ensures that OnBackPressedCallback is added only when LifecycleOwner is Lifecycle.state. STARTED. When the associated LifecycleOwner is destroyed, the activity removes registered callbacks to prevent memory leaks and make it applicable to fragments or other lifecycle owners that have a shorter life than the activity.

Here is an example

class MyFragment : Fragment() {

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

        // This callback is called only when MyFragment is at least Started
        val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
            // Intercept the return event
        }

        // This callback can be turned on and off here or in lambda above}... }Copy the code

You can provide multiple callbacks through addCallback(). When you do this, the callbacks are invoked in the reverse order in which they were added, meaning that the last callback added is the first callback given an opportunity to handle the return button event. For example, if you add three callbacks named 1, 2, and 3 in sequence, they will be called in order 3, 2, and 1, respectively.

Callbacks follow a “chain of responsibility” pattern. Each callback in the chain is invoked only if the previous callback is not enabled. This means that in the previous example, callback 2 was invoked only if callback 3 was not enabled. Callback 1 is invoked only if callback 2 is not enabled, and so on.

Note that when a callback is added through addCallback(), the callback is not added to the chain of responsibilities until LifecycleOwner enters the lifecy.state.started State.

Changing the enabled status of OnBackPressedCallback to make temporary changes (that is, changing the value of isEnabled) is highly recommended, as it preserves the above order, which is especially important if you have registered callbacks on multiple different nested lifecycle owners.

However, if you want to remove OnBackPressedCallback completely, remove() should be called. However, this is usually not necessary because the associated LifecycleOwner’s callback is automatically removed when it is destroyed.

Activity onBackPressed()

If you use onBackPressed() to handle the return button event, it is recommended that you use OnBackPressedCallback instead. However, if you are unable to make this change, the following rules apply:

  • When you call super.onBackPressed(), all the callbacks registered by addCallback will be passed.

  • OnBackPressed is always called regardless of any registered instance of OnBackPressedCallback.

Demo

The fragment return stack demo has been written and can be found here if you are interested.

See you next time.

About me

I am a Fly_with24

  • The Denver nuggets

  • Jane’s book

  • Github