代码之家  ›  专栏  ›  技术社区  ›  Jatin Bansal

使用startResolutionForResult()时,未在片段中调用onActivityResult();

  •  0
  • Jatin Bansal  · 技术社区  · 7 年前

    打开a GPS dialog 在片段中,当GPS被禁用,但当我单击按钮“确定”和“取消”时 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;
                    }
                }
            });             }
    

    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;
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   MJM    7 年前

    您已传递了意图为的活动对象,因此结果将在活动中返回,而不是在片段中返回

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

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