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

标签 java android android-fragments gps

当禁用 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/

相关文章:

Java解析txt文件意外字符

android - 我可以获取 Android 的 Google map 应用程序的源代码吗?

php - 如何在特定时间(计划时间)从php向android应用程序发送通知?

android - FrameLayout.onMeasure() 中的 NullPointerException

java - 如何在 grails 中获取数据库实例?

java - 我无法在 Eclipse 上给一个类命名

android - onSharedPreferenceChange 监听器 -> 在 fragment 中

android - 选项卡 fragment 内的 gridview

java - 来自 CursorWindow 的 java.lang.IllegalStateException : Couldn't read row 0, col -1

android - 检测在应用程序内部启动的 Activity