Use onBackPressed events effectively in Activity and Fragment

Tram Ho

Currently listening for the back event from the Android device is only done in Activity by overriding the onBackPressed () function and implementing the necessary methods. What about Fragment? We cannot use the same method as in Activity because there is simply no function to let us override. So how to listen for events back from the system, the most commonly used method is the method below

This approach can listen for the back event when the user presses the back button on the device at fragments that are directly interacting with the user, but on some devices with low firmware may not work and receive the back event. And if the Activity containing this Fragment together listens to the onBackPressed () event, it will easily lead to conflicts and the code will be difficult to manage.

With the above problems, in this article we would like to introduce a method of listening effectively to the onBackPressed () event to avoid some device problems, conflicts when listening to this event.

First, we can see that current Andoird application code is usually based on framework models such as MVP, MVVM, Clean Architect … We all build BaseActivity, BaseFragment … to implement special features. general calculation.

We will create the BaseFragment class that has the onBackPressed () function to listen for back events when needed, if any Fragment needs to handle this event just override it. The function has the following content

Returning a Boolean value to signal whether the Fragment is handling the onBackPressed () event. We will see its meaning in the function below.

As the first part of the article said in Fragment there is no onBackPressed () method to override, so where do we listen for this event? The answer is from the Activity that contains these Fragments. And here is how we listen for event back from Activity and pass back to Fragment.

We can understand the above function by performing the following steps:

  1. Create a handled variable to help confirm whether a Fragment needs to handle the onBackPressed event or not
  2. Run through the Fragments that this Activity manages
  3. Check whether Fragment is a child of BaseFragment or not
  4. If it is a child of BaseFragment then call Fragment’s onBackPressed function. It is on this line that will help to invoke the necessary functions when the user presses Back in that Fragment. If there is override onBackPressed function in Fragment, it returns true value, otherwise it returns false value by default.
  5. If Fragment has handle for Back event handling, then there is no need for Activity handling, return out of the function which avoids the collision when more parties listen to the Back event.
  6. If there are no Fragments to handle the Back event, use the Activity’s Back event

Based on this installation will help us easily manage pressing Back from the device, helping to avoid problems arising. Hope everyone can have better ways. The article here is to end

Share the news now

Source : Viblo