Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 调用startResolutionForResult时,onActivityResult()未在片段中执行_Android_Gps_Location_Onactivityresult - Fatal编程技术网

Android 调用startResolutionForResult时,onActivityResult()未在片段中执行

Android 调用startResolutionForResult时,onActivityResult()未在片段中执行,android,gps,location,onactivityresult,Android,Gps,Location,Onactivityresult,当我调用以编程方式使用GoogleAppClient在片段中启用gps时出现问题。。。 我的代码是 final Status status = result.getStatus(); final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) {

当我调用以编程方式使用GoogleAppClient在片段中启用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()没有在片段中执行。 问题在哪里???
帮帮我…..提前谢谢。

我今天刚写完一些同样的代码。您需要实现
onActivityForResult()
方法,但不幸的是
startResolutionForResult()
没有调用片段;它回调包含片段的活动

您必须在调用活动中(或在管理片段的任何位置)实现onActivityResult(),然后将结果转发给片段

我在ActivitityResult上的activities中做了类似的操作(FragmentBase只是我用于所有其他片段的基类,请确保在添加片段时标记它们):


请按照以下步骤操作:

一,。不活动:

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

    }
  • 零碎

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

  • 因为片段被放置在活动中,它们的生命周期与包含活动的生命周期紧密耦合

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

    在那里,您将收到来自父活动的回调。请执行任何您想发送或获取回调的操作。

    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)
    }
    
    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)
            }
        }
    }
    
    2。在您的活动中:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d(context!!, "onActivityResult: [From Fragment]: " + requestCode + ", " + resultCode)
    }
    
    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导航组件,这将起作用


    将这一行用于
    片段
    ,以获得活动结果
    onActivityResult

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

     status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
    

    当您需要解析
    状态
    可解析API异常
    时,我建议您利用
    活动。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)
    

    在该片段的Activity类中创建一个onActivityResult,并在其中放置一个调试器,以检查是否以getActivity()的形式启动活动。startActivityForResult(…)将其替换为startActivityForResult(…);。已执行活动的onActivityResult,但当我从活动调用片段的任何函数时,上下文总是null@SubhechhuKhanal我不是在调用startActivityForResult,我是在调用startResolutionForResult。如果正在调用活动的onActivityResult(),您不能从onActivityResult()调用片段的onActivityResult()吗活动范围??onActivityResult(请求代码、结果代码、数据)节省了我的时间。这是一个很好的答案,但最准确的答案是替换
    status.startResolutionForResult(getActivity(),REQUEST\u ID\u GPS\u PERMISSIONS)this.startinentsenderforresult(status.getResolution().getIntentSender(),REQUEST\u ID\u GPS\u PERMISSIONS,null,0,0,null)的片段中的code>。这样,您就不必在活动中添加onActivityResult,您的片段就会得到结果directly@JoseJet明亮的如果你把你的答案写下来,我会投赞成票。@JoseJet这正是我想要的。非常感谢@你的评论救了我一天!谢谢。它就像一个符咒!谢谢你的例子!已经找了好几个小时了谢谢!我在中找到了你删除的答案。这是帮助我使用新API的唯一答案。您在@KonstantinKonopko?面临的问题是什么?它非常适合我well@NikunjParadva这只有在我将
    onActivityResult
    添加到片段和活动中时才有效