What would be a good/recommended way of tying up the Google+ api client life cycle with the flow of a multi-activity app? Make the activities depend on the onConnected api client method to trigger its functionality, use it as a one-time only "activation" thing, or maybe something else entirely?
I am currently struggling to understand how to correctly use the Google+ sign in in my Android app, which has more than one activity.
The idea is, in a first phase, to use the G+ sign in just to authenticate the user and be able to get her email, to send notifications and stuff like that. Eventually I plan to roll out other Google functionality like maybe Maps or other Google Play services, so I think it's useful to implement it already.
However, my app is not behaving as expected, and I have narrowed down the issue to the fact that I have not yet understood the G+ sign in app cycle when more than one activity is present.
What is the correct or recommended way to implement this auth method? is there maybe a pattern of sorts that could guide me in the right direction?
For example, I have found a very simple diagram of the life cycle of the api client, but how does this relate to the app flow?
Initially I have a Login Activity, where I put the sign in button. Following Google's guide I am able to sign in, and when the onConnected method is called, I start the Home Activity (kinda like the dashboard or main screen of the app).
This works somewhat. For example, what would be a good way of handling the onStart and onStop for each activity? should I re-connect and re-authenticate the api client every time for every activity? So maybe its a good idea to have a BaseActivity to implement all this.
Another issue is, should I use the same api client object and pass it around somehow, or maybe store it in the Base Activity class? or should I be creating and initializing a new api client object every time?
How about just using the Login Activity to authenticate with G+ and then just get the email and store it in a local database, and flag the user as "authenticated" or "active" or something. That would prevent me from having to re-authenticate every time the app is closed or connection is suspended, even allowing for some battery savings.
The app is not really using G+ posting or any other functionality like that. Ideally it should work well offline, and only need connection for stuff like initial authentication or other one-time only things.
Any suggestions or pointers in the right direction are very much appreciated.
Edit: I have read every guide and tutorial I could find, that uses Google+, and every one of them addresses this from a single activity perspective. I would think this is a common enough problem that it would benefit from a pattern or at least a general guideline.
0. TL;DR
For the impatient coder, a working version of the following implementation can be found on GitHub.
After rewriting the login activity code several times in many different apps, the easy (and not so elegant) solution was create the Google API client as a Application class object. But, since the connection state affect the UX flow, I never was happy about with this approach.
Reducing our problem only to the connection concept, we may consider that:
1. Proxy Pattern
Since the
Connection
encapsulates theGoogleApiClient
, it will implement theConnectionCallbacks
andOnConnectionFailedListener
:Activities can communicate to the Connection class through the methods
connect
,disconnect
, andrevoke
, but their behaviors are decided by the current state. The following methods are required by the state machine:2. State Pattern
This is a behavioral pattern the allow an object to alter its behavior when its internal state changes. The GoF Design Patterns book describes how a TCP connection can be represent by this pattern (which is also our case).
A state from a state machine should be a
singleton
, and the easiest away of doing it in Java was to createEnum
namedState
as follows:The
Connection
class holds the context, i.e. the current state, which defines how theConnection
methodsconnect
,disconnect
, andrevoke
will behave:3. Singleton Pattern
Since there is not need to recreate this class repeatedly, we provide it as a singleton:
4. Observable Pattern
The
Connection
class extends JavaObservable
, so 1 or more activities can observe the state changes:Reconnecting for each activity is absolutely fine. Broadly there are 3 ways I've seen of people implementing this:
All of these work, and I've seen them all used in real world apps. The main thing to remember is to separate the 99% logic (user is either signed in or signed out, and you are being informed of that) from the relatively rare "signing in at this present moment" use-case. So for example, you might have onConnected/onConnection failed firing a lot, but mostly you are ignoring or just flipping a bit as to the state of the application. Only on a screen with a login button do you need the connection result resolution and onActivityResult stuff. Think of the google play services connection as being mostly about asking for the state of the user, rather than signing them in, and you should be fine.
I agree with Ian Barber's answer but to explain a little further, your
Activity
s should be considered in two types -Activity
s that resolve sign in, andActivity
s that require sign in.Most
Activity
s do not concern themselves with authenticating the user and will have the same logic in your app. They will create a GoogleApiClient, which connects to the Google Play services process running on the device and reads the cached sign-in state of the user - returningonConnected()
if the user is signed in, andonConnectionFailed()
if not. Most of yourActivity
s will want to reset your application state and start yourLoginActivity
if the user was not signed in. EachActivity
should maintain its own instance ofGoogleApiClient
since it is a lightweight object used to access the shared state held by the Google Play services process. This behaviour could, for example, be encapsulated in a sharedBaseActivity
class or a sharedSignInFragment
class, but each instance should have its ownGoogleApiClient
instance.Your
LoginActivity
needs to be implemented differently however. It should also create aGoogleApiClient
, but when it receivesonConnected()
indicating the user is signed in, it should start an appropriateActivity
for the user andfinish()
. When yourLoginActivity
receivesonConnectionFailed()
indicating the user is not signed in, you should attempt to resolve sign in issues withstartResolutionForResult()
.