onBackPressed() deprecated, What is the alternative

 

OnBackpressed is deprecated so what is the alternative?


onBackPressed() Deprecated, What is the Alternative?


If you have updated any of your application to API LEVEL 33, you have seen this error, onBackPressed is deprecated.


Maybe you have avare from Android 10 which starts from  (API_LEVEL 29) Now days the system provides gesture navigation feature. As ( most of the mobiles which start from API level 29 )the system supports gestures like swiping left to right to navigate back ( just like in IOS devices ). But this has led to unexpected behavior when it has been combined with horizontal swipes in applications.


OnBackpressed is deprecated so what is the alternative?


The problem is that the(Android confused about which kind of action is executed by the user) android system cannot differentiate if the gesture is for system back or for application back navigation. Which led to problems like closing an entire application instead of navigating back to the required activity or fragment. In other words, It cannot tell if the application handles the gesture.

So, What to use instead?


OnBackpressed is deprecated so what is the alternative?


So I have came across two solutions for achieving this problem, The first is to use the old function of onBackPressed, instead of calling the super.onBackPressed you can simply use isTaskRoot to check this activity is the last on stack or not, and the other solution is provided by the android itself the alternative way of handling backpressed events (which is introduce in API 29).


Use the isTaskRoot() method in the onCreate method of the SplashActivity of the app (the root Activity of the app) to determine whether it is the root Activity in the task stack, such as If it is, it will not do any processing, if it is not, it will be finished directly


Solution, Alternative Way of Handling Backpressed

Step 1 : Adding the required dependency in your project

implementation ‘androidx.activity:activity-ktx:1.6+'

Step 2:

Add android:enableOnBackInvokedCallBack="true” to your applications manifest <application> tag.

Step 3 :

Handling backpressed in  YourActivityClass



 







fun AppCompatActivity.onBackPressed(isEnabled: Boolean, callback: () -> Unit) {
    onBackPressedDispatcher.addCallback(this,
        object : OnBackPressedCallback(isEnabled) {
            override fun handleOnBackPressed() {
                callback()
            }
        })
}

// How To Use it : 
class ExampleActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_example)

        onBackPressed(true) {
            // do what do you want when get back 
        }
    }

}

Handling backpressed in  your Fragments

Make sure you have at least “androidx.activity:activity:1.+.+” at your dependencies, while working with fragments





fun FragmentActivity.onBackPressed(callback: () -> Unit) {
    onBackPressedDispatcher.addCallback(this,
        object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                callback()
            }
        }
    )
}

// How To Use it :
class ExampleFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requireActivity().onBackPressed {
            // do what do you want when get back 
        }
    }
}
onBackPressed() deprecated, What is the alternative

Post a Comment

Previous Post Next Post