gpt4 book ai didi

java - 使用startResolutionForResult()时,不会在fragment中调用onActivityResult();

转载 作者:行者123 更新时间:2023-12-02 02:20:06 27 4
gpt4 key购买 nike

当禁用 GPS 时,在 fragment 中打开 GPS 对话框,但当我单击按钮“确定”和“取消”时,OnActivityResult() 不会在 fragment 中调用.

任何人都可以指导我解决这个问题吗?这个问题只出现在fragment中,而不出现在activity中。

 public void openGpsDialog() {
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.

break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.

try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
homeActivity, REQUEST_LOCATION);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}); }

onActivityResult()

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case REQUEST_LOCATION:
switch (resultCode) {
case Activity.RESULT_OK:
Toast.makeText(HomeActivity.this, "gps enabled", Toast.LENGTH_SHORT).show();

break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to

break;
default:
break;
}
break;
}
}

最佳答案

您已经通过 Intent 传递了 Activity 对象,因此结果将在 Activity 中返回,而不是在 fragment 中

status.startResolutionForResult(homeActivity, REQUEST_LOCATION);

简单的解决方案是将您的响应从 Activity 传递到 fragment

Activity 代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}

或者

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getChildFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}

关于java - 使用startResolutionForResult()时,不会在fragment中调用onActivityResult();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621782/

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