3

In the Google+ Android PlusClient documentation it states that you should be executing .connect() and .disconnect() in onStart() and onStop().

I have several activities (about 8) which stack on top of eachother in various ways, and about 4 of those activities will require the PlusClient.

My base activity (which all those activities use).. I'm thinking of putting the PlusClient logic in there.

Without knowing the inner workings of the PlusClient itself I'm not sure how heavyweight it is to run connect()/disconnect() all the time in loads of activities. This was not clear in the documentation referenced above. The connect() / disconnect() methods are doing network requests in a background thread. It seems wasteful to me for the user to be clicking through all the activities and it doing multiple network requests doing the same thing.

I guess I am comparing this to the Facebook Android API which to me seems to have a "Session" type pattern which presents itself as being more efficient.

  • Is this really the best pattern to adopt for an app which browses through multiple Activities a lot?
  • Can these methods cope with not having a connection elegantly?
Eurig Jones
  • 8,226
  • 7
  • 52
  • 74

2 Answers2

1

PlusClient is designed to be a lightweight interface to Google Play services, see:

Access google plus client from multiple activities

Google Play services will manage an OAuth 2.0 access token for you which is roughly equivalent to a 'session'. When a specific Activity calls PlusClient.connect() and Google Play services already has an access token for the user for your app, it will immediately return in the onConnected() callback of your app.

As ianhanniballake says, any network calls needed to get the access token are carried out in the background, so they don't cause problems for your UI thread.

For an example of using PlusClient in a base activity take a look at the PhotoHunt example application:

https://github.com/googleplus/gplus-photohunt-client-android/tree/master/src/com/google/plus/samples/photohunt

Community
  • 1
  • 1
Lee
  • 3,972
  • 22
  • 17
0

connect() and disconnect() do all work in a background thread and therefore can be called in the UI thread (such as in onStart() or onStop()) without any affect on the performance of your application.

As long as you are calling connect()/disconnect() in onStart()/onStop(), respectively, the PlusClient will work on any number of Activities.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I know that connnect/disconnect methods are performant as I am assuming the G+ Team has sorted that :-). Apologies, my question is a bit vague because I guess I'm just surprised at the recommended pattern. It's probably not an ideal question for stackoverflow as it doesn't have a precise answer. I have updated my question a little bit. – Eurig Jones May 29 '13 at 19:56