keen
keen

Reputation: 3011

same GooglePlus client object in different activities android

I want to create a library for GooglePlus. For this I created multiple classes like login, posting, fetching user profile.

Do I have to implement onConnected, onDisconnected, onConnectionFailed ? Do I have to call plusClient.connect() in every class?

Currently If I connect() in first(login) class and then tries to post on use's account from different activity then it's giving a problem. I have to connect() it again even if i have done this in previous activity.

Is there a solution for this that I can use same plusClient object in multiple activities?

Thanks in advance.

Upvotes: 2

Views: 893

Answers (2)

Hounshell
Hounshell

Reputation: 5459

This is a situation that GoogleApiClient will help you with. Make the caller responsible for managing the lifecycle of GoogleApiClient. Have them pass it in to your library. There you can add a ConnectionCallbacks listener and perform your action in onConnected.

This also means that your library isn't responsible for handling connection failures. It's the caller's job in this case. Generally this isn't something you want in your library anyway, unless you're providing base Activity/Fragment classes. If that's the case then go ahead and handle those cases here. This way it's not sprinkled throughout your code.

Upvotes: 1

Lee
Lee

Reputation: 3992

The PlusClient class is designed to be lightweight so that there is no need to share a PlusClient instances across activity instances. See:

Access google plus client from multiple activities

That said, there is nothing which prevents you from doing this as long as you make sure that PlusClient.connect() and PlusClient.disconnect() are called at appropriate times if you manage your PlusClient as part of a service or application object.

Also, note that PlusClient has now been deprecated in favour of GoogleApiClient. For this discussion though, they are equivalent.

Upvotes: 3

Related Questions