Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 views
in Technique[技术] by (71.8m points)

android - Where should onActivityResult be handled, dialog fragment, fragment, or activity?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...