6

I want to make google sign in to my app, but I get this error.

startActivityForResult() deprecated in java

This is my code:

    val signInIntent = googleSignInClient.signInIntent

        startActivityForResult(signInIntent,RC_SIGN_IN)

And I know, I must use registerForActivityResult(); but I don't know how to use it.

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
hallooo
  • 61
  • 1
  • 2

3 Answers3

4

The code linked on Drive is close, but not quite correct. You have mixed up the resultCode and responseCode. You should check result.resultCode == Activity.RESULT_OK not result.resultCode == 100 in your callback for registerForActivityResult.

With the old startActivityForResult(intent,code) flow you would launch the activity with an integer code (e.g. 100), then check for that code in onActivityResult to make sure you were handling the right response. With the callbacks that integer request/response code is no longer necessary or used.

private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // handle the response in result.data
    }
}

then you use it as others have indicated

val signInIntent = googleSignInClient.signInIntent
launcher.launch(signInIntent)

There are also some examples of this on the answers here

Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • https://stackoverflow.com/questions/69153970/startactivityforresult-deprecated-for-facebook-sign-in – Bolt UIX Sep 12 '21 at 17:55
  • For LoginManager, it appears to be calling `startActivityForResult` within the third party library itself. If that is the case, there is nothing you can do to remove the `onActivityResult` call for that path until LoginManager is updated to not use the deprecated code path. Others have asked this too [here](https://stackoverflow.com/questions/67297326/how-to-use-facebook-sign-in-callbackmanager-with-onactivityresult-deprecated) – Tyler V Sep 12 '21 at 18:01
1

It's not an error for now. It's warning about deprecated method. registerForActivityResult() reuired ActivityResultContract. In this case it's StartActivityForResult(). All you need is declare a variable

private val launcher = registerForActivityResult(StartActivityForResult()){result ->  //Intent
....
}

and launch intent when needed with launcher.launch(signInIntent)

Stanislav Bondar
  • 6,056
  • 2
  • 34
  • 46
  • 2
    this solution is not working for google sign in –  Sep 12 '21 at 14:52
  • https://drive.google.com/file/d/1SSn4QqhlKVh0Y5C5i5oPSg9lrAA7EsbZ/view?usp=sharing here is my full code –  Sep 12 '21 at 14:55
1

It's a warning. However it leaves you feeling weird and worrying about future versions.

So I did it this way.

 val signInIntent: Intent = mGoogleSignInClient.signInIntent
 launcher.launch(signInIntent)

The important thing here is to source the data that uses the google intent thingi.

 private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result ->
    if (result.resultCode == Activity.RESULT_OK) {
        val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
        handleSignInResult(task)
    }
}

So that's it. It works and no warning. I also made a question and it's a mystery, so it would be great if you check it out. Cannot generate app bundle... something with mergeReleaseResources


ouflak
  • 2,458
  • 10
  • 44
  • 49