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

时间:2016-02-25 04:57:27

标签: android google-api onactivityresult

当我在片段中调用onActivityResult时,我在父活动中拥有的GoogleApiClient是"未连接"。

@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);
    }
}

但是,如果我在同一片段类的按钮中执行相同的操作,那么事物就会连接起来。

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

        }
    });

AFAIK这应该调用相同的活动,那么为什么客户端在第一个而不是第二个中断开连接?

//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();

}

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:0)

你是否在onStop或者活动的破坏中断开了api?