Error in Fragment: “Already managing a GoogleApiClient with id 0”

后端 未结 10 1849
独厮守ぢ
独厮守ぢ 2020-12-13 08:16

Everything works right the first time, if you launch a second time you see this error:

FATAL EXCEPTION: main
Process         


        
相关标签:
10条回答
  • 2020-12-13 08:55

    You should call stopAutoManage() in the onPause() method of your Fragment like so:

    @Override
    public void onPause() {
        super.onPause();
    
        mGoogleApiClient.stopAutoManage(getActivity());
        mGoogleApiClient.disconnect();
    }
    
    0 讨论(0)
  • 2020-12-13 08:59

    This works for me to avoid crashing issues using kotlin

    private lateinit var googleApiClient: GoogleApiClient
    

    And then just verify if the variable is already initialized

     if(!::googleApiClient.isInitialized) {
                googleApiClient = GoogleApiClient.Builder(context)
                        .enableAutoManage(activity, this)
                        .addApi(Auth.GOOGLE_SIGN_IN_API, options)
                        .build()
      }
    
    0 讨论(0)
  • 2020-12-13 09:02

    To avoid further issue

    @Override
    public void onStop() {
        super.onStop();
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.stopAutoManage((Activity) context);
            mGoogleApiClient.disconnect();
        }
    }
    
    0 讨论(0)
  • 2020-12-13 09:02

    You should call stopAutoManage() in the onDestroy() method of your fragment like so:

         @Override
            public void onDestroy() {
                 super.onDestroy();
                 mGoogleApiClient.stopAutoManage(getActivity());
                 mGoogleApiClient.disconnect();
              }
    
    0 讨论(0)
提交回复
热议问题