Blog Infos
Author
Published
Topics
Author
Published

While Targeting Android 13 , OnbackPressed Override Function is deprecated😢. Usually, we used to intercept on backpress and add animations. we all know, this onBackPressed function will call as soon as the Back button is clicked (Hardware) or back pressed from the navigation gesture. But, now it’s time to change/move….

Hmm., No matter what “Change is the only constant..!!”

Why is the OnBackPress function Deprecated?🤔

In Android 13, we have a new Preview feature called, “Predictive back gesture.” Currently, it’s not available for end-Users. Because of this feature, they deprecated the onBackPressed Function in Android 13. So we are going to migrate and make our apps to get ready for Android 13’s predictive back gesture.

Note: As Android Team mentioned in the note, Predictive back gesture it’s only visible for developers for testing purposes while Targeting Android 13.

Now, its time to Migrate.. 🤞

Let’s see our old way of usage,

override fun onBackPressed() {
    //showing dialog and then closing the application..
    showDialog()
}

private fun showDialog(){
    MaterialAlertDialogBuilder(this).apply {
        setTitle("are you sure?")
        setMessage("want to close the application ?")
        setPositiveButton("Yes") { _, _ -> finish() }
        setNegativeButton("No", null)
        show()
    }
}

The above code will show the popup when back press is triggered. If the user clicks on yes, the app will close. If the user clicks on No, the app will not close. Pretty simple example right?

OnBackPressedCallback(enabled) //enabled is true or false

In this callback, enabled is a default parameter which we have to pass by any condition if needed. If you want by default, you can enable it by passing true.

The below code is the total replica of our old way onbackPressed() function.

Now to migrate the above code, we have to follow 2 Simple Steps

Step 1 :

Create a onBackPressed Callback.

private val onBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            //showing dialog and then closing the application..
            showDialog()
        }
   }

STEP 2 :

Add the callback in the onBackPressedDispatcher(). This should be inside the onCreate function and it’s a good practice to use the bottom of setContentView or after Binding the layout.

override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      // adding onbackpressed callback listener.
      onBackPressedDispatcher.addCallback(this,onBackPressedCallback)
  }

Job Offers

Job Offers


    Senior Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Android Test Automation Engineer

    Komoot
    Remote
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, ,

From Scoped Storage to Photo Picker: Everything to know about Storage

Persistence is a core element of every mobile app. Android provides different APIs to access or expose files with different tradeoffs.
Watch Video

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocate
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer ad ...
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocat ...
Google

Jobs

Final MainActivity code :

class MainActivity : AppCompatActivity() {

    private val onBackPressedCallback = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            //showing dialog and then closing the application..
            showDialog()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // adding onbackpressed callback listener.
        onBackPressedDispatcher.addCallback(this,onBackPressedCallback)
    }

    private fun showDialog(){
        MaterialAlertDialogBuilder(this).apply {
            setTitle("are you sure?")
            setMessage("want to close the application ?")
            setPositiveButton("Yes") { _, _ -> finish() }
            setNegativeButton("No", null)
            show()
        }
    }
}

Note : If suppose onBackPressedDispatcher or OnBackPressedCallback are not found for you guys, please check your activity ktx version which should be at least or more than 1.6.1.

If you create a new project in Android studio, the above Note is not mandatory.

dependencies { //build.gradle app level
    implementation 'androidx.activity:activity-ktx:1.6.1'
}

That’s it..! now we replaced the deprecated onBackPressed function. 🙂

If you learnt something new, Please don’t forget to Follow, like, and support me 👍😊

This article was originally published on proandroiddev.com on November 26, 2022

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Today, We will explore the New notification 🔔 runtime permission that was added in…
READ MORE
blog
In this article, we will explore Android 13’s new clipboard UI, as well as…
READ MORE
blog
App launcher icons, the very first interaction that someone has with your app is…
READ MORE
blog
Android 13 (API 33) introduces a new tool called photo picker 🖼. Today, we…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu