I have a simple use case where:
Activity1 create a fragment1
fragment1 after creation notify to activity that it is created and update its activity1 views.
activity1 after getting notification update fragment1 views.
I am using rxandroid , sublibrary rxlifecycle
components and android , but i am still in learning phase , there was not even rx-lifecycle
tag on stackoverflow , so i am still struggling to understand the flow of this library..
Edit
I prefer not to use EventBus , it's just like everyone shouting at everyone to do something, so Rxjava Observable approach will be much useful
For posting information from fragment to activity, you should use an event bus for informing activity about fragment creation (replacement to the callbacks and the mess they created).
Sample code for event bus with RxJava is:
Then from your fragment you can call:
from
onAttach()
oronViewCreated()
or any place you prefer.Also, in activity you will need to put the following code to listet to your event bus:
For the second part, i.e. to update the fragment from activity, I won't recommend using this method as it will lead to unnecessary complexity, Instead use the "original way" as:
updateView(Object obj)
onNext()
, get the desired fragment asSampleFragment fragment = (SampleFragment)getSupportFragmentManager().findFragmentByTag("TAG");
fragment.updateView(obj);
Hope this helps.
Two points to consider:
Just because you use an EventBus does not mean that it needs to be global. You can have multiple event buses if you want, and you can just share a single one between two components (Activity and Fragment).
There are several examples in the RxJava documentation that show how to implement event bus functionality using RxJava
By Using an event bus, you can simplify things greatly, by disassociating the whole process from the Android lifecycle.