简体   繁体   English

片段中的startActivityForResult不会调用,并且将其关闭

[英]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,它对我很有用,希望对您有所帮助。

The answer i found it here 我在这里找到答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android片段中的startActivityForResult()vs getActivity()。startActivityForResult() - startActivityForResult() vs getActivity().startActivityForResult() in Android Fragment 调用startActivityForResult()时,Android 8.1关闭活动 - Android 8.1 Closes Activity when startActivityForResult() is called Android-片段内的RecyclerViewAdapter中的startActivityForResult() - Android - startActivityForResult() from a RecyclerViewAdapter inside a Fragment 从startactivityforresult返回时,Android片段消失 - Android fragment disappear when return from startactivityforresult 片段中的startActivityForResult - startActivityForResult in fragment Android Fragment教程强制关闭 - Android Fragment tutorial force closes 来自片段活动的片段调用startactivityforresult,但notifydatasetchanged不起作用 - fragment from fragment activity calls startactivityforresult but notifydatasetchanged does not work 从 Fragment 调用 startActivityForResult 不会调用 onActivityResult - Call startActivityForResult from Fragment doesn't call onActivityResult 后续调用startActivityForResult后,Android应用程序挂起 - Android app hangs after subsequent call to startActivityForResult startActivity()是否始终调用startActivityForResult()? - Does startActivity() always call through to startActivityForResult()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM