How to correctly use Google Plus Sign In with multiple activities?

Reconnecting for each activity is absolutely fine. Broadly there are 3 ways I’ve seen of people implementing this:

  1. Implement mostly in a baseactivity, and have the others extend that. This is connect/disconnect in each activity, but with code in only one place.
  2. Implement connect/disconnect in a fragment, and include that in activities where auth is needed. This is helpful if you already have a baseactivity you can’t extend (e.g. some games cases).
  3. Implement a service to connect/disconnect. This can fire a broadcastintent or similar if sign in is required.

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.

Leave a Comment