Skip to content

Commit 3c7a32c

Browse files
SUPERCILEXsamtstern
authored andcommitted
Add startActivityForResult explanation (#435)
1 parent f329eb1 commit 3c7a32c

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

auth/README.md

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ Twitter app as reported by the [Twitter application manager](https://apps.twitte
9191
</resources>
9292
```
9393

94-
In addition, if are using Smart Lock or require a user's email, you must enable the
94+
In addition, if you are using Smart Lock or require a user's email, you must enable the
9595
"Request email addresses from users" permission in the "Permissions" tab of your app.
9696

9797
## Using FirebaseUI for Authentication
9898

9999
Before invoking the FirebaseUI authentication flow, your app should check
100100
whether a
101-
[user is already signed in](https://firebase.google.com/docs/auth/android/manage-users#get_the_currently_signed-in_user) from a previous session:
101+
[user is already signed in](https://firebase.google.com/docs/auth/android/manage-users#get_the_currently_signed-in_user)
102+
from a previous session:
102103

103104
```java
104105
FirebaseAuth auth = FirebaseAuth.getInstance();
@@ -141,12 +142,22 @@ If no customization is required, and only email authentication is required, the
141142
can be started as follows:
142143

143144
```java
145+
// Choose an arbitrary request code value
146+
private static final int RC_SIGN_IN = 123;
147+
148+
// ...
149+
144150
startActivityForResult(
145151
// Get an instance of AuthUI based on the default app
146152
AuthUI.getInstance().createSignInIntentBuilder().build(),
147153
RC_SIGN_IN);
148154
```
149155

156+
To kick off the FirebaseUI sign in flow, call startActivityForResult(...) on the sign in Intent you built.
157+
The second parameter (RC_SIGN_IN) is a request code you define to identify the request when the result
158+
is returned to your app in onActivityResult(...). See the [response codes](#response-codes) section below for more
159+
details on receiving the results of the sign in flow.
160+
150161
You can enable sign-in providers like Google Sign-In or Facebook Log In by calling the
151162
`setProviders` method:
152163

@@ -216,28 +227,31 @@ supported.
216227
```java
217228
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
218229
super.onActivityResult(requestCode, resultCode, data);
219-
if (resultCode == RESULT_OK) {
220-
// user is signed in!
221-
startActivity(new Intent(this, WelcomeBackActivity.class));
222-
finish();
223-
return;
224-
}
225-
226-
// Sign in canceled
227-
if (resultCode == RESULT_CANCELED) {
228-
showSnackbar(R.string.sign_in_cancelled);
229-
return;
230-
}
231-
232-
// No network
233-
if (resultCode == ResultCodes.RESULT_NO_NETWORK) {
234-
showSnackbar(R.string.no_internet_connection);
235-
return;
230+
// RC_SIGN_IN is the request code you passed into startActivityForResult(...) when starting the sign in flow
231+
if (requestCode == RC_SIGN_IN) {
232+
if (resultCode == RESULT_OK) {
233+
// user is signed in!
234+
startActivity(new Intent(this, WelcomeBackActivity.class));
235+
finish();
236+
return;
237+
}
238+
239+
// Sign in canceled
240+
if (resultCode == RESULT_CANCELED) {
241+
showSnackbar(R.string.sign_in_cancelled);
242+
return;
243+
}
244+
245+
// No network
246+
if (resultCode == ResultCodes.RESULT_NO_NETWORK) {
247+
showSnackbar(R.string.no_internet_connection);
248+
return;
249+
}
250+
251+
// User is not signed in. Maybe just wait for the user to press
252+
// "sign in" again, or show a message.
236253
}
237-
238-
// User is not signed in. Maybe just wait for the user to press
239-
// "sign in" again, or show a message.
240-
}
254+
}
241255
```
242256

243257
Alternatively, you can register a listener for authentication state changes;

0 commit comments

Comments
 (0)