为什么当我尝试访问 OnActivityResult 而不是其他地方时,GoogleApiClient 会断开连接?

Why is GoogleApiClient disconnected when I try to access in OnActivityResult but not elsewhere?

当我在片段中调用 onActivityResult 时,父 activity 中的 GoogleApiClient 是 "not connected"。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Constants.REQUEST_PLACE_PICKER) {

        if (resultCode == Activity.RESULT_OK) {
            /* User has picked a place, extract data.
               Data is extracted from the returned intent by retrieving a Place object from
               the PlacePicker.
             */
            final Place place = PlacePicker.getPlace(data, getActivity());

            SharedPreferences prefs = getActivity().getSharedPreferences(Constants.SHARED_PREFS, Context.MODE_MULTI_PROCESS);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("address"+id, address.toString()).commit();
            editor.putString("name"+id, name.toString()).commit();
            Log.d(TAG, "Just picked a location, id=" + id);

            ((AllinOneActivity getActivity()).addGeofenceFromListposition(id); 
     getActivity()).addGeofencesButtonHandler(); // <-- This code that checks mGoogleCLientApi.connected()


            mRecyclerView.setAdapter(new LocationListAdapter(getGymLocations(), mListener, this));
        } else {
            Log.d(TAG, "resultCode is wrong " + "resultCode");

        }

    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

但是,如果我在同一个片段中的按钮中执行相同的操作 class,则该操作已连接。

    //in onCreateView
    mFab = (FloatingActionButton) view.findViewById(R.id.fab);
    mFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onListFragmentInteraction(); //<-- Work fine!

        }
    });

据我所知,这应该调用相同的 activity,那么为什么客户端在第一个中断开连接而不是第二个?

//In parent Activity
public void onListFragmentInteraction(){
    addGeofencesButtonHandler();
}
public void addGeofencesButtonHandler() {
    Log.d(TAG, "Adding geoFencesButtonHandler click");
    if (!mGoogleApiClient.isConnected()) {
        Toast.makeText(this, "not connected in addgeofence", Toast.LENGTH_SHORT).show();
        return;
    }

    try {
        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                // The GeofenceRequest object.
                getGeofencingRequest(),
                // A pending intent that that is reused when calling removeGeofences(). This
                // pending intent is used to generate an intent when a matched geofence
                // transition is observed.
                getGeofencePendingIntent()
        ).setResultCallback(this); // Result processed in onResult().
    } catch (SecurityException securityException) {
        // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
        logSecurityException(securityException);
    }
}

protected void onStart() {
        super.onStart();
    mGoogleApiClient.connect();

}

protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();

}

您是否在 activity 的 onStop 或 ondestroy 中断开了 api?

当您连接 GoogleApiClient 时,它不会立即连接。您必须注册连接回调侦听器才能知道连接何时真正完成。

在您的情况下,发生的情况是 Android 在您与 onStart() 的连接完成之前 调用了 onActivityResult()。 onActivityResult() 在 onStart() 和 onResume() 之间被调用,但您不能假设在调用 onActivityResult() 时客户端连接已完成。

您需要做的是记住成员变量中 onActivityResult() 的结果,然后检查您的连接侦听器中的该值,以了解何时可以安全地将您的 GoogleApiClient 对象与该数据一起使用。