[英]in fragment startActivityForResult does not call and it closes it sleft Android
I know there is so many question related to this but I still not find any solution i have tried all the things. 我知道有很多与此相关的问题,但是我仍然没有找到所有尝试过的解决方案。
from onActivityResult() not called when Activity started from Fragment I find 我发现从片段开始活动时未从onActivityResult()调用
getActivity().startActivityForResult(galleryIntent, PICK_IMAGE);
with 与
startActivityForResult(galleryIntent, PICK_IMAGE);
onActivityResult is not being called in Fragment 片段中未调用onActivityResult
super.onActivityResult //on activity
I have also done this but still it's not working. 我也这样做了,但仍然无法正常工作。
does any body has solution? 有什么办法解决吗?
thanks 谢谢
If you are using getActivity().startActivityForResult(galleryIntent, PICK_IMAGE);
如果您使用的是
getActivity().startActivityForResult(galleryIntent, PICK_IMAGE);
then you have to call onActivityResult()
in your activity class where you are adding your fragment. 那么您必须在要添加片段的活动类中调用
onActivityResult()
。
And If you are using startActivityForResult(galleryIntent, PICK_IMAGE);
并且如果您使用的是
startActivityForResult(galleryIntent, PICK_IMAGE);
inside the fragment then you have to call onActivityResult()
in your fragment class. 在片段内部,则必须在片段类中调用
onActivityResult()
。
In the Activity you're creating set the result using an intent. 在要创建的活动中,使用意图设置结果。 onActivityResult() will catch the intent for you to process.
onActivityResult()将捕获您要处理的意图。 Here's an example of returning an
int
value. 这是一个返回
int
值的示例。
public void sendResultToFragment(int result) {
Intent resultIntent = new Intent();
resultIntent.putExtra("int-result", result);
setResult(Activity.RESULT_OK, resultIntent);
}
Keep the super.onActivityResult(...)
override in your main activity where your fragment lives. 将
super.onActivityResult(...)
覆盖在片段所在的主要活动中。
You must override onActivityResult() in your Fragment 您必须在片段中覆盖onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case PICK_IMAGE:
int result = data.getInt("int-result");
// Do stuff
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
Hi i had the same problem and i realized that i opened the activity with the flag FLAG_ACTIVITY_NO_HISTORY 嗨,我遇到了同样的问题,我意识到我用标志FLAG_ACTIVITY_NO_HISTORY打开了活动
new Intent(SplashActivity.this, StartedActivity.class).setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
The documentation mentions that when the noHistory attribute is set to true or the flag in the intent, then finish() is called on the Activity when the user navigates away from the Activity. 文档中提到,当noHistory属性设置为true或意图中的标志时,当用户离开Activity时,会在Activity上调用finish()。
So just remove and the problem will be solved. 因此,只需删除即可解决问题。
Btw i have the onActivityResult just in the fragment, it works great for me hope it helps. 顺便说一句,我只是在片段中有onActivityResult,它对我很有用,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.