[英]onActivityResult() not executing in fragment when i call startResolutionForResult
Problem when i am calling to enable gps programatically using GoogleApiClient into fragment... My code is..当我调用以编程方式使用 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;
}
and my onActivityResult is我的 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;
}
but my onActicvityResult() not executing in fragment.但我的 onAccticvityResult() 没有在片段中执行。 Where is the problem???
问题出在哪里??? Help me.....Thanks in advance.
帮助我.....提前谢谢。
Use this Line for Fragment
to get result in onActivityResult
将此行用于
Fragment
以在onActivityResult
中获得结果
startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_ID_GPS_PERMISSIONS, null, 0, 0, 0, null);
insted of安装的
status.startResolutionForResult(getActivity(), REQUEST_ID_GPS_PERMISSIONS);
As fragments are placed inside activities and their life cycle tightly coupled to the life cycle of the containing activity.由于片段被放置在活动中,它们的生命周期与包含活动的生命周期紧密耦合。
1) In Activity: 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);
}
}
Now container activity of fragment will provide intent data, request code and result to the fragment so to get data and result in fragment you have to override onActivityResult(int requestCode, int resultCode, Intent data)
in fragment as well现在片段的容器活动将为片段提供意图数据、请求代码和结果,因此要获取数据并生成片段,您还必须在片段中覆盖
onActivityResult(int requestCode, int resultCode, Intent data)
2) In Fragment 2)在片段中
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
there you will get callback from your parent activity.Do whatever you want to send or to get callback.在那里你会从你的父母活动中得到回调。做任何你想发送或得到回调的事情。
When you need to resolve the Status
or the ResolvableApiException
, I suggest you to leverage the activity.registerForActivityResult
API in place of startResolutionForResult
:当您需要解决
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)
I just finished writing some of this same code today.我今天刚刚完成了一些相同的代码。 You're on the right track with needing the
onActivityForResult()
method implemented, but unfortunately startResolutionForResult()
doesn't call back to the fragment;你在正确的轨道上需要实现
onActivityForResult()
方法,但不幸的是startResolutionForResult()
没有回调片段; it calls back to the activity that contains the fragment.它回调包含片段的活动。
You must implement onActivityResult()
in the calling activity (or wherever your fragments are being managed), and then forward that result to your fragment.您必须在调用活动(或管理您的片段的任何地方)中实现
onActivityResult()
,然后将该结果转发给您的片段。
I did something like this in my activities onActivityResult
(FragmentBase is just the base class I'm using for all my other fragments, make sure you tag your fragments when you add them):我在我的活动
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);
}
}
Solution for 2019 [Kotlin] [AndroidX] 2019 年解决方案 [Kotlin] [AndroidX]
1. In Your Fragment: 1. 在你的片段中:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.d(context!!, "onActivityResult: [From Fragment]: " + requestCode + ", " + resultCode)
}
2. In Your Activity: 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)
}
}
}
This will work if you're using Android Navigation Component.
如果您使用的是 Android 导航组件,这将起作用。
Follow the below steps :请按照以下步骤操作:
1 . 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);
}
}
In Fragment在片段中
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // Now in fragment will triggger Here you can do work }
If you are running this code in Fragment than don't use startResolutionForResult().如果您在 Fragment 中运行此代码,则不要使用 startResolutionForResult()。 Instead use startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);
而是使用 startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);
and overrider onaActivityResult() in your fragment.并在您的片段中覆盖 onaActivityResult()。 Result will be delivered to this method only.
结果将仅传递给此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.