应该在哪里onActivityResult处理,对话的片段,片段或活动? [英] Where should onActivityResult be handled, dialog fragment, fragment, or activity?

查看:138
本文介绍了应该在哪里onActivityResult处理,对话的片段,片段或活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我有一个片段,用户可以添加图片,无论是从画廊或从相机。我创建了一个警告对话框,并放置在一个DialogFragment。当用户选择一个选项,我叫startActivityForResult。我的问题是,在理想情况下应该这样的结果来处理? (即我应该在哪里放置onActivityResult?)在DialogFragment类,主机片段,或者主机的活动?有没有关系呢?

In my android app, I have a fragment where the user can add a picture, either from gallery or from camera. I've created an alert dialog and placed it in a DialogFragment. When the user chooses an option, I call startActivityForResult. My question is, where should ideally this result be handled? (i.e. where should i place onActivityResult?) In the DialogFragment class, the host fragment, or the host activity? Does it matter?

推荐答案

onActivityResult()将在活动第一次调用。之后,它会伸出手来的片段,如果你调用 super.onActivityResult()在你的活动。

onActivityResult() will be invoked first on the Activity. After that it will be reached out to the Fragments, if you call super.onActivityResult() in your Activity.

这是因为碎片的模块化设计。如果结果进来,活动处理它与未处理的结果,如果有的话,可以伸手的片段。

This is because of the modular design of Fragments. If a result comes in, the Activity handles it and the unhandled results, if any, can be reached out to the Fragments.

要回答你的问题:决定在哪里是有意义的处理结果就您的应用程序设计/ code。如果你处理它的片段并将结果发送回活动通过回调FE,您可以在活动直接处理它。

To answer your question: Decide where it makes sense to handle the results in respect of your app design / code. If you handle it in the Fragment and send the results back to the Activity through a callback f.e., you can handle it directly in the Activity.

这篇关于应该在哪里onActivityResult处理,对话的片段,片段或活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆