当我调用startResolutionForResult时,onActivityResult()不在片段中执行

Rak*_*aha 4 gps android location onactivityresult

问题,当我打电话使用GoogleApiClient以编程方式启用gps片段...我的代码是..

 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.
                        getCurrentLocation();
                        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(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
                        } 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;
                }
Run Code Online (Sandbox Code Playgroud)

我的onActivityResult是

 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.
                        getCurrentLocation();
                        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(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
                        } 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;
                }
Run Code Online (Sandbox Code Playgroud)

但我的onActicvityResult()没有在片段中执行.问题出在哪儿???帮帮我.....提前谢谢.

Nik*_*dva 12

使用此行Fragment获取结果onActivityResult

startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_ID_GPS_PERMISSIONS, null, 0, 0, 0, null);
Run Code Online (Sandbox Code Playgroud)

插入的

 status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
Run Code Online (Sandbox Code Playgroud)


Luc*_*cky 11

由于片段被放置在活动内部并且它们的生命周期与包含活动的生命周期紧密耦合.

1)在活动中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment frg = getSupportFragmentManager().findFragmentById(R.id.fragment_container_main);
    if (frg != null) {
        frg.onActivityResult(requestCode, resultCode, data);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在片段的容器活动将为片段提供意图数据,请求代码和结果,以便获取数据并生成片段,您还必须onActivityResult(int requestCode, int resultCode, Intent data)在片段中覆盖它们

2)在片段中

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

}
Run Code Online (Sandbox Code Playgroud)

在那里你将从你的父活动获得回调.做你想要发送的任何东西或获得回调.

  • 这是一个很好的答案,但最准确的是在你的片段中替换`status.startResolutionForResult(getActivity(),REQUEST_ID_GPS_PERMISSIONS);``this.startIntentSenderForResult(status.getResolution().getIntentSender(),REQUEST_ID_GPS_PERMISSIONS,null, 0,0,0,null);`.这样您就不必在活动中放置onActivityResult,您的片段将直接获得结果 (19认同)

gia*_*ise 10

当您需要解决Status或 时ResolvableApiException,我建议您利用activity.registerForActivityResultAPI 代替startResolutionForResult

val launcher = activity.registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            // User accepted
        } else {
            // User didn't accepted
        }
    }

val intentSenderRequest = IntentSenderRequest.Builder(exception.resolution).build()
launcher.launch(intentSenderRequest)
Run Code Online (Sandbox Code Playgroud)


swe*_*rly 3

我今天刚刚写完一些相同的代码。您走在正确的轨道上,需要onActivityForResult()实现该方法,但不幸的startResolutionForResult()是没有回调该片段;它回调包含该片段的活动。

onActivityResult()您必须在调用活动(或管理片段的任何地方)中实现,然后将该结果转发到您的片段。

我在我的活动中做了类似的事情onActivityResult(FragmentBase 只是我用于所有其他片段的基类,请确保在添加片段时标记它们):

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        switch (requestCode){
            case LocationHelper.LOCATION_ENABLER_ID:
                FragmentBase mapFrag = (FragmentBase) fragmentManager.findFragmentByTag(FragmentBase.MAP_FRAGMENT);
                ((FragmentMap)mapFrag).returnFromSettings();
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }
Run Code Online (Sandbox Code Playgroud)