How To Replace OnActivityResult With New Activity Result API?

Hi All,

I wonder if someone may be able to assist with my struggles :slightly_frowning_face:, I’m new to Kotlin, after implementing facebook login following the documentation into my application I have realised that onActivityResult() is deprecated (though it still works for the timebeing).

I am trying to adapt my code to work with the new Activity Result API’s (I have saw the suggestions online to use registerForActivityResult) however, my issue is that in the code from the documentation we are only calling a method ParseFacebookUtils.logInWithReadPermissionsInBackground rather than handling the calling of the activity nested within this method. As such, it seems I am unable to capture the result of the activity being started within this method as I cannot change the call to use registerForActivityResult and unsure how this can be achieved without being able to change the code.

It is possible I am missing something simple here (which may be due to my inexperience with Kotlin) however, I am completely stuck and been going around in circles for nearly a week to no avail :frowning: so thought asking the experts would be my best option at this point :slight_smile:

Would you mind to share the piece of code that you are struggling with?

Hi @davimacedo

The piece of code I’m struggling with is below:

 val permissions: List<String> = listOf("public_profile", "email")
        ParseFacebookUtils.logInWithReadPermissionsInBackground(this,permissions) { user: ParseUser?, err: ParseException? ->
            when {
                err != null -> {
                    Log.e("FacebookLoginExample", "done: ", err)
                    Toast.makeText(this, err.message, Toast.LENGTH_LONG).show()
                user == null -> {
                    Toast.makeText(this, "The user cancelled the Facebook login.", Toast.LENGTH_LONG).show()
                    Log.d("FacebookLoginExample", "Uh oh. The user cancelled the Facebook login.")
                user.isNew -> {
                    Toast.makeText(this, "User signed up and logged in through Facebook.", Toast.LENGTH_LONG).show()
                    Log.d("FacebookLoginExample", "User signed up and logged in through Facebook!")
                else -> {
                    Toast.makeText(this, "User logged in through Facebook.", Toast.LENGTH_LONG).show()
                    Log.d("FacebookLoginExample", "User logged in through Facebook!")
                    showAlert("Oh, you!", "Welcome back!")



More specifically the ParseFacebookUtils.logInWithReadPermissionsInBackground, it seems the activity is nested within this method so I am unable to change the way the activity is started to adapt it to the new android activity API’s.

When you say “I have realised that onActivityResult() is deprecated”, do you see any deprecation message on your logs? Is it due to an internal call that the Parse SDK performs? Or a line of code you have on your app? Would you mind to share?

The deprecated method onActivityResult is part of androidx.activity.ComponentActivity and appears with a strikethrough it when trying to override, the warning that shows in my files is as below:

'onActivityResult(Int, Int, Intent?): Unit' is deprecated. Overrides deprecated member in 'androidx.activity.ComponentActivity'. Deprecated in Java

To implement the Facebook login as per the documentation we must override this method to call another method, as below:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        ParseFacebookUtils.onActivityResult(requestCode, resultCode, data)
        super.onActivityResult(requestCode, resultCode, data)

From what I gather deprecated methods don’t instantly stop working as to give developers time to adapt their code, my issue now is how can we capture the activity result without using onActivityResult. given the activity we need to capture the result of is nested within a ParseFacebookUtils method.