简体   繁体   English

从片段的子节点返回Activity的onActivityResult

[英]return to Activity's onActivityResult from fragment's child

ActivityCat starts DialogFragmentLoves with show() . ActivityCat使用show()启动DialogFragmentLoves。 Then DialogFramentLoves starts ActivityDog with startActivityForResult . 然后DialogFramentLoves使用startActivityForResult启动ActivityDog。 When ActivityDog returns, naturally it calls the onActivityResult method that is inside the Fragment. 当ActivityDog返回时,自然会调用Fragment内的onActivityResult方法。 But I want the data to go to ActivityCat. 但是我希望数据进入ActivityCat。 But doing as follows inside DialogFragmentLoves does not work: 但是在DialogFragmentLoves中执行以下操作无效:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    getActivity().onActivityResult(requestCode, resultCode, data);
    dismiss();
}

What's the correct way of doing this? 正确的做法是什么?

Actually the documentation of startActivityForResult in Fragment says 实际上Fragment中的startActivityForResult文档说

Call startActivityForResult(Intent, int) from the fragment's containing Activity. 从包含片段的Activity调用startActivityForResult(Intent,int)。

So actually it should work as it is now. 所以实际上它应该像现在一样工作。 Maybe it's enough to not override onActivityResult in your fragment? 也许在片段中不覆盖onActivityResult就足够了吗? Or maybe you're not calling super.onActivityResult()? 或者,也许您没有调用super.onActivityResult()? Another option might be to call getActivity().startActivityForResult instead...although this should be exactly what the fragment's method seems to do... 另一个选择可能是改为调用getActivity()。startActivityForResult ...虽然这应该恰好是片段的方法似乎要做的...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM