7
    @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) – Brandon 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
  • 1
    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
  • Did you get your answer @user16388371 ? I am looking answer for the same question. – Rajesh Koshti Aug 10 '22 at 09:38

6 Answers6

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
  • 3,004
  • 1
  • 11
  • 23
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

AShX
  • 358
  • 3
  • 14
  • 4
    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
  • @Max You can build the intent with signed in account's info. https://stackoverflow.com/a/75711587/1135631 – Amit Jayant Mar 12 '23 at 07:18
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
    }


}
  • 3
    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
  • 30,962
  • 25
  • 85
  • 135
Yokich
  • 1,247
  • 1
  • 17
  • 31
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()
0

If someone looking to use requestPermission function with the new activity launcher way, here is a solution..

GoogleSignIn.requestPermission() internally calls startActivityForResult with an intent containing signed-in account's info.

So, what you can do is, create an intent with the signed-in account info and call the activity result launcher with that intent. This will request the scope from the user.

Intent for the scope you need

fun getIntentForFitnessScope(context: Context): Intent {
        val signedInAccount: GoogleSignInAccount? = GoogleSignIn.getLastSignedInAccount(context)
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(Scope("https://www.googleapis.com/auth/fitness.activity.write"))
                .setAccountName(signedInAccount?.email!!)
                .build()
            return GoogleSignIn.getClient(context, gso).signInIntent
    }

Activity result launcher

val googleSignInLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            // Do something with result.data()
        }
    }

Launch the activity result launcher

googleSignInLauncher.launch(getIntentForFitnessScope(context))
Community
  • 1
  • 1
Amit Jayant
  • 2,501
  • 2
  • 29
  • 38