Getting exception "Only GoogleApiClient control with id 0" when using TabLayout Viewpager

I want to integrate Google signup using TagLayout ViewPager snippets. The ViewPager contains two fragments: LoginFragment and RegistrationFragment, and both contain a Google Sign-up button.

The problem is that I get the following error on startup.

java.lang.IllegalStateException: already managing GoogleApiClient with id 0

End the error log -

java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0
at com.google.android.gms.common.internal.zzac.zza(Unknown Source)
at com.google.android.gms.internal.zzzt.zza(Unknown Source)
at com.google.android.gms.common.api.GoogleApiClient$Builder.zzf(Unknown Source)
at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source)
at com.naturesouq_shopping.fragment.RegistrationFragment.buildGoogleApiClient(RegistrationFragment.java:174)
at com.naturesouq_shopping.fragment.RegistrationFragment.getIds(RegistrationFragment.java:140)
at com.naturesouq_shopping.fragment.RegistrationFragment.onCreateView(RegistrationFragment.java:85)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1969)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:20236)

I'm trying to execute code

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

 // Configure sign-in to request the user ID, email address, and basic
 // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        buildGoogleApiClient();

     // .....
 }

private synchronized void buildGoogleApiClient() {
    /** build_client */
   try {
       mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
               .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
               .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
               .build();
   } catch (Exception e) {
       e.printStackTrace();
   }
}

and

@Override
public void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    } 

}

@Override
public void onStop() {
    Log.d("GARG", "***** on Stop ***** ");
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        Log.d("GARG", "***** on Stop mGoogleApiClient disconnect ***** ");

        mGoogleApiClient.stopAutoManage(getActivity());
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

Is there any other way to connect the Google API client? Any idea on where I am doing wrong and right?

+4
source share
2 answers

, googleApiClient, . :

if(mGoogleApiClient == null || !mGoogleApiClient.isConnected()){
 try {
   mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
           .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
           .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
           .build();
 } catch (Exception e) {
   e.printStackTrace();
 }
}
+8

enableAutoManage :

. , stopAutoManage (FragmentActivity) .

enableAutoManage clientId, 0. , clientId 0, .

, FragmentActivity, , GoogleApiClient. , FragmentActivity GoogleApiClient? .

FragmentActivity, ViewPager, GoogleApiClients, RegistrationFragment LoginFragment. , FragmentActivity AdFragment GoogleApiClient, , RegistrationFragment GoogleApiClient.

:

  • clientIds enableAutoManage, LoginFragment ReservationFragment.
  • , stopAutoManage. ViewPager.
  • , GoogleApiClient FragmentActivity, ViewPager, RegistrationFragment LoginFragment. , GoogleApiClient Activity, GoogleApiClient Activity :

GoogleApiClient googleApiClient;

  @Override
  void onAttach(Activity activity) {
      super.onAttach(activity);
      googleApiClient = activity.getGoogleApiClient();
  }

  @Override
  void onDetach() {
      super.onDetach();
      googleApiClient = null;
  }
+6

Source: https://habr.com/ru/post/1669269/


All Articles