Reputation: 10977
I'm using GPGS in my app for achievements. I would like to connect to GPGS on startup so I can load and set user achievements. But I want to connect only if the user earlier connected to it by clicking a connect button in the app.
Use case 1:
- User opens app
- App does not connect to GPGS
Use case 2:
- User opens app
- Clicks button to connect to GPGS
- Closes app
days later
- User opens app
- App connects to GPGS
I used to store a boolean flag in the shared prefs to know if the app is authorized. The problem is that I have no way to know when the user signs out in the achievements Activity or disconnects the app in the devices google settings.
What I would like to do is something like
if(mGoogleApiClient.isAutorized(){
mGoogleApiClient.connect();
}
Any ideas how I can figure out if the app is authorized?
Upvotes: 0
Views: 167
Reputation: 199825
When you call mGoogleApiClient.connect()
, this is not visible to the user unless the login was successful (and the banner appears with their profile picture).
You have to specifically call result.startResolutionForResult()
with the result
from onConnectionFailed
to start the user visible login flow. As long as you don't call that until the user clicks the login button then you'll have the behavior you want.
Note that if you are using the GameHelper
/BaseGameActivity
classes, you may need to disable the default behavior of auto-login and only call beginUserInitiatedSignIn()
(which does the above startResolutionForResult()
call for you) when the user specifically clicks the login button.
The FAQ states:
[4] Why is GameHelper/BaseGameActivity attempting to sign in on application startup?
The default behavior of BaseGameActivity and GameHelper is to show the user the sign-in flow (consent dialogs, etc) as soon as your application starts. Naturally, once the user signs in for the first time, they won't see the consent flow again, so it will be a seamless experience. It is important for the user to sign in as early as possible so your application can take advantage of the Google Play Games API right away (for example, saving the user's progress using Cloud Save, unlocking achievements, etc). If the user cancels the sign-in flow, BaseGameAcitivity/GameHelper will remember that cancellation. If the total number of cancellations reaches a predefined maximum (by default, 3), the user will no longer be prompted to sign in on application startup. If that happens, they can still sign in by clicking your application's Sign In button, if you provide one.
[5] I don't like the new "auto sign in" feature of GameHelper. How can I disable it?
To disable this feature and return to the old behavior, you can edit GameHelper.java and set the DEFAULT_MAX_SIGN_IN_ATTEMPTS constant to 0, or call GameHelper.setMaxAutoSignInAttempts(0) at runtime, before calling GameHelper.setup() (or, correspondingly, from your Activity's onCreate method).
Upvotes: 1