How to make onActivityResult get called on Nested Fragment
2015-08-20 17:03
921 查看
One of the common problem we always meet in the world of Fragment is: although we could call
Why does this happen? That's because Fragment is not first designed to be nested. Once its capability was expanded, the architecture behind Fragment couldn't cover all the case. And we developers have to handle the problem case by case by ourselves.
But don't worry, we already have a sustainable and robust workaround for this problem. Ok, let's start !
Once Activity was navigated back, the result will be sent to Activity's
The problem is: Activity could send the result to only the Fragment that has been attached directly to Activity but not the nested one. That's the reason why onActivityResult of nested fragment would never been called no matter what.
The problem, as described above, is the request could be sent from nested fragment but couldn't be received properly. Thus there is no need to do those things in Fragment. Let them be all done in Activity level.
So we will call
As a result, all of the result received will be handled at the single place:
Question is how to send the Activity Result to Fragment?
Due to the fact that we couldn't directly communicate with all of the nested fragment in the normal way, or at least in the easy way. And another fact is, every Fragment knows that which requestCode it has to handled since it is also the one that call startActivityForResult. So we choose the way to"broadcast to every single Fragment that is active at time. And let those Fragments check requestCode and do what they want."
Talk about broadcasting, LocalBroadcastManager could do the job but the mechanic is the way too old. I choose another alternative, an EventBus, which has a lot of choices out there. The one that I chose was Otto from square. It is really good at performance and robustness.
First of all, add a following line in
In the Otto way, let's create a Bus Event as a package carry those Activity Result values.
ActivityResultEvent.java
And of course, also create a Singleton of Event Bus which will be used to send a package from an Activity to all of active Fragments.
ActivityResultBus.java
You may notice that I also create a custom method named
And then we will override onActivityResult on Activity and add a following line to send the package to the bus once the result is received.
In Fragment part, we need to listen to the package sent from Activity. We could do it easily in Otto way like this.
That's all.
With this solution, it could be applied for any single fragment whether it is nested or not. And yes, it also covers all the case! Moreover, the codes are also nice and clean.
Add a dependency in
In case you use Fragment from android.app.*, please add the following instead.
To enable it, just simply override method
For Fragment, you could simple extends
As I said. Easy, huh?
Hope that this article is helpful to you all. Best wishes to you all =)
startActivityForResultdirectly from Nested Fragment but it appears that
onActivityResultwould never been called which brought a lot of trouble to handle Activity Result from Nested Fragment.
Why does this happen? That's because Fragment is not first designed to be nested. Once its capability was expanded, the architecture behind Fragment couldn't cover all the case. And we developers have to handle the problem case by case by ourselves.
But don't worry, we already have a sustainable and robust workaround for this problem. Ok, let's start !
Architecture behind Fragment's startActivityForResult
Although we could callstartActivityForResultdirectly from Fragment but actually mechanic behind are all handled by Activity. Once you call startActivityForResult from a Fragment,requestCode will be changed to attach Fragment's identity to the code. That will let Activity be able to track back that who send this request once result is received.
Once Activity was navigated back, the result will be sent to Activity's
onActivityResultwith the modified requestCode which will be decoded to original requestCode + Fragment's identity. After that, Activity will send the Activity Result to that Fragment through
onActivityResult. And it's all done.
The problem is: Activity could send the result to only the Fragment that has been attached directly to Activity but not the nested one. That's the reason why onActivityResult of nested fragment would never been called no matter what.
The Solution
This behavior is one of the most popular issue in town. We could found a lot of thread related to this in stackoverflow. There are a lot of workaround provided by people there. Anyway none of them is sustainable enough to be used in any case (at least all of those that I discovered). So we spend a day research all the mechanic behind and try to find the way to cover all the cases available. And finally we found one!The problem, as described above, is the request could be sent from nested fragment but couldn't be received properly. Thus there is no need to do those things in Fragment. Let them be all done in Activity level.
So we will call
getActivity().startActivityForResult(...)from Fragment instead of just
startActivityResult(...)from now on. Like this:
onActivityResultof the Activity that Fragment is placed on.
Question is how to send the Activity Result to Fragment?
Due to the fact that we couldn't directly communicate with all of the nested fragment in the normal way, or at least in the easy way. And another fact is, every Fragment knows that which requestCode it has to handled since it is also the one that call startActivityForResult. So we choose the way to"broadcast to every single Fragment that is active at time. And let those Fragments check requestCode and do what they want."
Talk about broadcasting, LocalBroadcastManager could do the job but the mechanic is the way too old. I choose another alternative, an EventBus, which has a lot of choices out there. The one that I chose was Otto from square. It is really good at performance and robustness.
First of all, add a following line in
build.gradleto include Otto to our project:
ActivityResultEvent.java
ActivityResultBus.java
postQueuein the bus object. This one is used to send a package into the bus. And the reason why we have to do it this way is because we have to delay a package sending a little bit since at the moment that Activitiy's onActivityResult has been called, the Fragment is not become active yet. So we need to let Handler send those commands to the queue of Main Thread with handler.post(...) like coded above.
And then we will override onActivityResult on Activity and add a following line to send the package to the bus once the result is received.
Fragment's onActivityResultwill be called from now on ! You can now just simply override onActivityResult, check the requestCode and do what you want.
Limitation
There is just only one limitation. Don't use the same requestCode in different Fragment. As you can see, every single Fragment that is active at time will be receive the package. If you use the same requestCode in different Fragment, it may delivers the wrong outcome. Except that you intend to do it, you can.Make it easy with StatedFragment
Good news! The code we described in this article are already included in our StatedFragment in version 0.9.3 and above. You could now use it easily like this:Add a dependency in
build.gradle
onActivityResultin the Activity and add a following line:
StatedFragment.
onActivityResultwill be now useful.
Hope that this article is helpful to you all. Best wishes to you all =)
相关文章推荐
- .net 下存取Excel的利器(第三方)
- CodeForces 560B Gerald is into Art
- oracle优化的相关文章链接
- oracle表在线重定义
- 虚拟化--043 vsphere 在VMware workstation 9.0中安装Windows server 2012 和 Hyper-v
- BST的插入、删除、查找操作
- Quick to learn Python rules
- 如何安装vmtools工具
- linux进程学习
- 虚拟化--042 vsphere Vapp&resource pool内存和cpu资源参数详细介绍
- java 泛型generic type
- hdu 4974——A simple water problem
- Bootstrap 栅格系统
- jstat命令详解
- 跨平台base64数据传输注意问题
- easybcd 支持 windows 10 和 ubuntu 14.04 双系统启动
- Struts2(三)——数据在框架中的数据流转问题
- 自我的一点介绍(七夕礼物)
- Popupwindow实现透明
- Android 在程序中重启APP的方法