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

时间:2017-07-14 07:03:54

标签: android gps 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;
                }

我的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;
                }

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

6 个答案:

答案 0 :(得分:12)

将此行用于Fragment,以在onActivityResult中获得结果

startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_ID_GPS_PERMISSIONS, null, 0, 0, 0, null);

已安装

 status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);

答案 1 :(得分:10)

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

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

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

2)在片段中

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

}

在那里你将从你的父活动获得回调。做你想发送的任何内容或获得回调。

答案 2 :(得分:2)

当您需要解析StatusResolvableApiException时,建议您使用activity.registerForActivityResult API代替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)

答案 3 :(得分:0)

我今天刚刚写完了一些相同的代码。您需要实施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);
        }
    }

答案 4 :(得分:0)

按照以下步骤操作:

1。 InActivity:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        YourFragment fragment = (YourFragment ) getSupportFragmentManager().findFragmentByTag("TAG_NAME");
        if (fragment != null) {
            fragment .onActivityResult(requestCode, resultCode, data);
        }

    }
  1. 在片段中

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     {
     // Now in fragment will triggger Here you can do work
    
    }
    

答案 5 :(得分:0)

2019年解决方案[Kotlin] [AndroidX]

1。在您的片段中:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    Log.d(context!!, "onActivityResult: [From Fragment]: " + requestCode + ", " + resultCode)
}

2。在您的活动中:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    Log.d(this, "onActivityResult: [From Activity]:  " + requestCode + ", " + resultCode)
    val navHostFragment = supportFragmentManager.fragments.first() as? NavHostFragment
    if(navHostFragment != null) {
        val childFragments = navHostFragment.childFragmentManager.fragments
        childFragments.forEach { fragment ->
            fragment.onActivityResult(requestCode, resultCode, data)
        }
    }
}
  

如果您使用的是Android导航组件,则可以使用。