I want to be able to update my score for my leaderboard in a different activity other than the one that creates the GoogleApiClient obj ( the main activity ).
What is th
Ok I thought i would answer my own Question as i have found a method that currently suits me. Alot of this has come from another answered question found here!
This method uses the basegameutil so going from Lee's answer from the link i can just have both my activities extend the BaseGameActivity and add the required methods.
I then create a new instance of the GoogleApiClient in my second activity and then get the api client like this:
mGoogleApiClient = getApiClient();
i can now pass this around my second activity and call the methods as i wish.
As Lee States in his answer, you can have multiple instances of the google api client and still wont be asked to sign in for each of them as it knows you are already signed in.
If you need to be constantly connected to the GoogleApiClient then I would probably make a singleton class to handle GoogleApiClient calls (http://en.wikipedia.org/wiki/Singleton_pattern) or make a background service that would run for the whole app lifecycle (https://developer.android.com/training/best-background.html).
From the developer documentation:
GoogleApiClient is used with a variety of static methods. Some of these methods require that GoogleApiClient be connected, some will queue up calls before GoogleApiClient is connected; check the specific API documentation to determine whether you need to be connected.
If you don't need to send the data immediately GoogleApiClient is going to queue the data to be sent for you and just send it in the primary Activity.
You could use Application class, an android-oriented singleton pattern class and it could be your base class for your ApiClient calls.
Here is a link to the API documentation, and here are some examples as well as arguments why use Application over Singleton.
My real test is: 1. Can not pass an GoogleApiClient as an object to another service or activity, by using it as a Intent parameter. 2. Let use Singleton class solve it as below: + Step 1: Make Singleton class which include GoogleApiClient object as data member. Please do like this:
import com.google.android.gms.common.api.GoogleApiClient;
public class MyGoogleApiClient_Singleton {
private static final String TAG = "MyGoogleApiClient_Singleton";
private static MyGoogleApiClient_Singleton instance = null;
private static GoogleApiClient mGoogleApiClient = null;
protected MyGoogleApiClient_Singleton() {
}
public static MyGoogleApiClient_Singleton getInstance(GoogleApiClient aGoogleApiClient) {
if(instance == null) {
instance = new MyGoogleApiClient_Singleton();
if (mGoogleApiClient == null)
mGoogleApiClient = aGoogleApiClient;
}
return instance;
}
public GoogleApiClient get_GoogleApiClient(){
return mGoogleApiClient;
}
}
Step 2: In main Activity class do initialize a GoogleApiClient object then call getInstance with mGoogleApiClient as a parameter of Singleton base class.
Step 3: In another service/Activity which you want to pass GoogleApiClient object to, just call getInstance(null) of Singleton baseclass, and call get_GoogleApiClient to get desired GoogleApiClient object.
If you got any problem, please contact me thienpham2008@gmail.com.