Closed eriuzo closed 8 years ago
I'm not familiar with English, I'm very sorry.onRxViewRegistered() is only called in onActivityStarted(),I registered Fragment B on Activity A success use
@Override public void onRxViewRegistered() { dispatcher.subscribeRxView(fragment); }
Suppose that all fragments are all inside Activity A,Not fragment inside fragment. Here's my solution:
@Override
public void onAttachFragment(Fragment fragment)
{
super.onAttachFragment(fragment);
if (fragment instanceof RxViewDispatch) {
RxViewDispatch viewDispatch = (RxViewDispatch) fragment;
viewDispatch.onRxViewRegistered();
AppApplication.getInstance().getRxFlux().getDispatcher().subscribeRxView(viewDispatch);
}
}
@Override
public void onRxViewRegistered() {
rxFlux = AppApplication.getInstance().getRxFlux();
apiActionCreator = ApiActionCreator.get(rxFlux.getDispatcher(), rxFlux.getSubscriptionManager());
apiStore = ApiStore.get(rxFlux.getDispatcher());
}
@Override
public void onDetach(){
super.onDetach();
rxFlux.getDispatcher().unsubscribeRxView(this);
}
Note that Fragment B C D should not be instantiated from one Fragment, if so the following C or D might not be registered. Because the class name is used as a tag in subscribeRxView:
public <T extends RxViewDispatch> void subscribeRxView(final T object) {
final String tag = object.getClass().getSimpleName();
Subscription subscription = rxStoreMap.get(tag);
...
}
Hope @skimarxall got some time to fix this
Thanks for comments, I think I will try to change my approach.
@skimarxall so can I clarify that even though Fragment
s can implement RxViewDispatch
, only Activities
can register a Fragment
? I think my confusion stems from that.
@eriuzo if you get the instance of the dispatcher you can register any class that implements RxViewDispatch.
How do you get the instance is up to you. The thing is that then you have to take care of unregister this instance manually. RxFlux handles the activity lifecycle and does it for you.
The approach @eprendre took is right. What I need to change is using the tag to store the subscription. Since if we have two fragments of the same class they will overlap each other.
I will do that on the next PR
@coolfire2015 yes onRxViewRegistered is called after the Activity is registered, that happens onStart. do you have a question or was just to answer @eriuzo?
@skimarxall No question ,thanks
@skimarxall
Fragment
B, C, D and the transitions among them are managed by Activity
A. (the OnClickListener
in Fragment
B now calls a public method in Activity
A). So I dont need for @eprendre 's solution for now.RxFlux
only handles Activity
lifecycle. Do you plan to handle Fragment
lifecycle sometime in the future? (Instead of doing it manually as described by @eprendre)Hi,
Hi, I am using 0.3.2 (my own fork, as soon as you merge that PR, i will upgrade to 0.4.0)
I have use case like this:
Activity A loads Fragment B. Fragment B, on button press, can replace itself with Fragment C or D.
Fragment C and D isnt getting onRxStoreChanged, presumably because they arent registered.
I registered them on Fragment B, but looks like it isnt working. Is there a way to register them without creating an instance on Activity A? (the one instantiating C and D is B)
Also, just want to confirm, as of 0.3.2, the way to register fragments is like this right?