gpt4 book ai didi

Android - GoogleApiClient - 不连接 fragment 负载

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:46 26 4
gpt4 key购买 nike

在 Fragment onCreateview 中,连接 GoogleAPIClient,但它不会触发 onConnected 调用,但如果我转到另一个 fragment 并返回,它会触发.

Couldnt find why? any help would be appreciated as i got stuck ?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(), GOOGLE_API_CLIENT_ID, this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
mPlaceArrayAdapter.setGoogleApiClient(mGoogleApiClient);
Log.i(LOG_TAG, "Google Places API connected.");
}

最佳答案

void onCreate (Bundle savedInstanceState) Called to do initial creation of a fragment.

您应该添加此 onCreate 部分

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(), GOOGLE_API_CLIENT_ID, this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();

Log.d(TAG, "onCreate");
}

然后

@Override
public void onResume() {
super.onResume();
mGoogleApiClient .connect();
Log.d(TAG, "onResume");
}

这将是检查 onPause() 状态的好方法。

@Override
public void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected())
mGoogleApiClient.disconnect();
Log.d(TAG, "onPause");
}

关于Android - GoogleApiClient - 不连接 fragment 负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50034587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com