Java 使用startResolutionForResult()时未在片段中调用onActivityResult();
当GPS被禁用时,在片段中打开Java 使用startResolutionForResult()时未在片段中调用onActivityResult();,java,android,android-fragments,gps,Java,Android,Android Fragments,Gps,当GPS被禁用时,在片段中打开GPS对话框,但当我单击按钮“OK”和“Cancel”时,则OnActivityResult()不调用片段 有谁能指导我解决这个问题吗?? 这个问题只发生在片段中,而不发生在活动中 public void openGpsDialog() { PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocat
GPS对话框
,但当我单击按钮“OK”和“Cancel”时,则OnActivityResult()
不调用片段
有谁能指导我解决这个问题吗??
这个问题只发生在片段中,而不发生在活动中
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;
}
}
}); }
您已经传递了活动对象,所以结果将在活动中返回,而不是在片段中
status.startResolutionForResult(homeActivity, REQUEST_LOCATION);
简单的解决方案是将您的响应从活动传递到片段
活动代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
或
您已经传递了活动对象,所以结果将在活动中返回,而不是在片段中
status.startResolutionForResult(homeActivity, REQUEST_LOCATION);
简单的解决方案是将您的响应从活动传递到片段
活动代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
或
您是否尝试过
getActivity().startResult()的解决方案代码>它将调用onActivityResult(),或者如果您想调用OnActivityForResult的片段,则删除'super.onActivityResult(请求代码、结果代码、数据);`并在调用时调用try don call superi我已经尝试了此操作,但没有响应。您是否尝试了getActivity()。StartResult()的解决方案代码>它将调用onActivityResult(),或者如果您想调用OnActivityForResult的片段,则删除'super.onActivityResult(请求代码、结果代码、数据);`谢谢你的回答,但我正在使用viewpager并使用FragmentPagerAdaptor在viewpager上设置fragment,所以我如何设置fragment ID谢谢你的回答,但我正在使用viewpager并使用FragmentPagerAdaptor在viewpager上设置fragment,那么如何设置片段id呢