passing the GoogleApiClient obj from one activity to another

前端 未结 4 1180
旧巷少年郎
旧巷少年郎 2021-02-19 04:54

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

4条回答
  •  暖寄归人
    2021-02-19 05:33

    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.

提交回复
热议问题