onBackPressed() deprecated, What is the alternative
by@Codeblogs•August 31, 2022
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.
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 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).
Source: → Codeblogs
[Java Issues Fixed in Kotlin
Nowadays, Kotlin has been considered as a modern statically typed programming language used by over 60% of professional…story.codeblogs.info](https://story.codeblogs.info/java-issues-fixed-in-kotlin "story.codeblogs.info/java-issues-fixed-in-k..")
[Java Issues Fixed in Kotlin
Nowadays, Kotlin has been considered as a modern statically typed programming language used by over 60% of professional…story.codeblogs.info](https://story.codeblogs.info/java-issues-fixed-in-kotlin "story.codeblogs.info/java-issues-fixed-in-k..")
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
}
}
}