4
    @Override **//depricated**
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
         if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_OAUTH_REQUEST_CODE) {
                insertAndVerifySession();
            }
        }
    }

GoogleSignIn.requestPermissions(
                fragment,
                REQUEST_OAUTH_REQUEST_CODE,
                GoogleSignIn.getLastSignedInAccount(context),
                fitnessOptions);

What's the alternative of onActivityResult for GoogleSignIn in Fragment?

  • Does this answer your question? [OnActivityResult method is deprecated, what is the alternative?](https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative) – Beast Jul 06 '21 at 06:57
  • there is a new approach, get familiar with [`ActivityResultContracts`](https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts) class from AndroidX. [HERE](https://medium.com/quinbay/activityresultcontract-activityresultapis-in-androidx-935bf1fc9ed2) you have some explanations and tutorial – snachmsm Jul 06 '21 at 05:45
  • Thanks for the reply, we are familiar with this approach, but my concern is how we can use it with GoogleSignIn? `GoogleSignIn.requestPermissions( fragment, REQUEST_OAUTH_REQUEST_CODE, GoogleSignIn.getLastSignedInAccount(context), fitnessOptions);` –  Jul 06 '21 at 05:57

5 Answers5

4

As here said, you can get sign in intent by calling getSignInIntent

ActivityResultLauncher<Intent> exampleActivityResult= registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
    @Override
    public void onActivityResult(ActivityResult result) {
        if (result.getResultCode() == Activity.RESULT_OK) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData());
handleSignInResult(task);
        }
    }
});



//call
   exampleActivityResult.launch(mGoogleSignInClient.getSignInIntent());

Update - 30-11-2021

here is the new method by google !

Naveen Avidi
  • 2,528
  • 1
  • 11
  • 21
1

One of the ways is to register ActivityResultContracts, i used similar code in my project:

 //declare ActivityResultLauncher<Intent> 
 ActivityResultLauncher<Intent> ResultLauncher;

inside onAttach or onCreate do the assignment

ResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                 ...//do stuff on result here
                      /* 
                  There are no request codes
                              You can put request code in extra and Unpack it as your string/int e.t.c
                      Intent data = result.getData();
                       assert data != null;
                        String req_code = data.getStringExtra("reqCode");
                             if(req_code.equal(REQUEST_OAUTH_REQUEST_CODE){
                                     ...do stuff
                                           }
                         */
                    }
                });

to launch activity just use .launch (intent)

Intent intent = new Intent(getContext(), SomeClass.class);
 ResultLauncher.launch(intent);

Also you can check this answer for similar question and how to handle it in Kotlin/Java
Hopes it helps

GremlinShX
  • 416
  • 5
  • 14
  • 2
    This doesn't explain how to request permissions from GoogleSignIn with the new API. Where would I get the Intent from? – Max Sep 24 '21 at 14:29
0

After on activity result is deprecated we use someActivityResultLauncher.launch

In Java:

 Intent intent = new Intent(this, Example.class);
 someActivityResultLauncher.launch(intent);

In Kotlin:

val intent = Intent(this, Example::class.java)
resultLauncher.launch(intent)

For fetching results we use registerForActivityResult

In Java:

 ActivityResultLauncher<Intent> exampleActivityResult= registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                    // There are no request codes in this method 
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent data = result.getData();
                }
            }
        });

In Kotlin:

var exampleActivityResult= registerForActivityResult(StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
    // There are no request codes in this method 
    val data: Intent? = result.data
    }


}
  • 2
    Thanks for the reply, we know this approach, the main concern is how we can use google signing infragment? `GoogleSignIn.requestPermissions( fragment, REQUEST_OAUTH_REQUEST_CODE, GoogleSignIn.getLastSignedInAccount(context), fitnessOptions); ` –  Jul 06 '21 at 06:05
  • How use it in fragments? – Mukhit Nov 29 '21 at 06:31
0

There doesn't seem to be replacement for the deprecated onActivityResult, but to use the new contracts. Here's a neat snippet to handle sign in to google in a contained class.

https://gist.github.com/wintren/b866232ca01cfbfb4592fe909b989efd

After importing the GoogleSignInActivityContract.kt class the Activity code would be:

    // Before onCreate
    private val googleSignInRequest = registerForActivityResult(
        GoogleSignInActivityContract(),
        ::onGoogleSignInResult
    )

    private val googleSignInOptions: GoogleSignInOptions
        get() = 
            GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.GooglePlayServiceKey_WebClient))
            .requestEmail()
            .requestProfile()
            .build()

    fun triggerFunction {
        googleSignInRequest.launch(googleSignInOptions)
    }

There's more inte the snippet for handling the result.

Dharman
  • 26,923
  • 21
  • 73
  • 125
Yokich
  • 1,117
  • 1
  • 16
  • 30
0

If anyone is still looking for a solution to request permission in a fragment:

private lateinit var googleAuthActivityResultLauncher: ActivityResultLauncher<Intent>

private val fitnessOptions: FitnessOptions by lazy {
    FitnessOptions.builder()
        .addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_WRITE)
        .build()
}

onAttach:

googleAuthActivityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
        val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
        task.addOnCompleteListener {
            if (task.isSuccessful) handleRequest()
        }
    }

where you want to launch the intent:

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .addExtension(fitnessOptions)
            .build()
        val googleSignInClient = GoogleSignIn.getClient(requireContext(), gso)
        val signInIntent = googleSignInClient.signInIntent

        googleAuthActivityResultLauncher.launch(signInIntent)

and do not forget onDestroyView:

googleAuthActivityResultLauncher.unregister()