当我调用 startResolutionForResult 时,onActivityResult() 没有在片段中执行
Posted
技术标签:
【中文标题】当我调用 startResolutionForResult 时,onActivityResult() 没有在片段中执行【英文标题】:onActivityResult() not executing in fragment when i call startResolutionForResult 【发布时间】:2017-12-19 04:28:36 【问题描述】:当我调用以使用 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;
但是我的 onAccticvityResult() 没有在片段中执行。 问题出在哪里??? 帮助我.....提前谢谢。
【问题讨论】:
在该片段的 Activity 类中创建一个 onActivityResult 并在其中放置一个调试器进行检查 如果您以 getActivity().startActivityForResult(...) 的形式开始您的活动,请将其替换为 startActivityForResult(...);。 onActivityResult 执行的 Activity 但是当我从 Activity 调用片段的任何函数时,上下文总是为空 @SubhechhuKhanal 我不是在调用 startActivityForResult,而是在调用 startResolutionForResult。 如果你的活动 onActivityResult() 被调用,你不能从活动的 onActivityResult() 调用片段的 onActivityResult() 吗? fragment.onActivityResult(requestCode, resultCode, data) 【参考方案1】:将此行用于Fragment
以获得onActivityResult
的结果
startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_ID_GPS_PERMISSIONS, null, 0, 0, 0, null);
插入
status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
【讨论】:
@KonstantinKonopko 你面临什么问题?,它对我很有效 @NikunjParadva 这仅在我将onActivityResult
添加到我的片段和活动中时才有效【参考方案2】:
由于片段被放置在活动中,它们的生命周期与包含活动的生命周期紧密耦合。
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);
在那里你会从你的父活动中得到回调。做任何你想发送或得到回调的事情。
【讨论】:
节省我的时间。时间。 这是一个很好的答案,但最准确的方法是将片段中的status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
替换为this.startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_ID_GPS_PERMISSIONS, null, 0, 0, 0, null);
。这样你就不必在你的活动中放置一个 onActivityResult 并且你的片段会直接得到结果
@JoseJet 辉煌。如果你把你写的作为答案,我会投赞成票。
@JoseJet 这正是我所期待的。非常感谢!
@JoseJet 你的评论拯救了我的一天!非常感谢。它就像一个魅力!【参考方案3】:
当您需要解析Status
或ResolvableApiException
时,我建议您利用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)
【讨论】:
感谢您的示例!找了好几个小时 谢谢!我在***.com/a/63296989/2914140 中找到了您已删除的答案。这是帮助我使用新 API 的唯一答案。【参考方案4】:我今天刚刚完成了一些相同的代码。你在正确的轨道上需要实现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);
【讨论】:
【参考方案5】: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 导航组件,这将起作用。
【讨论】:
【参考方案6】:按照以下步骤:
1 .不活动:
@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
【讨论】:
以上是关于当我调用 startResolutionForResult 时,onActivityResult() 没有在片段中执行的主要内容,如果未能解决你的问题,请参考以下文章
当我使用 curl 命令调用服务时出现异常“UT000004:getResponseChannel() 已被调用”
com.netflix.zuul.exception.ZuulException:当我调用 POST 方法调用时出现转发错误