-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add @RootFragment annotation for @EBean #1636
Description
Hi!
Currently, the only way to access the parent Fragment in an @EBean is by writing a setter in the bean manually, or by making a @FragmentById/@FragmentByTag public in the @RootContextannotated Activity. The code is not that beautiful, so why not create an @RootFragment annotation?
It would work the same way as @RootContext, excepted that it would not need to be re-injected if the Fragment instance is retained (see setRetainInstance() & getRetainInstance()) and the field is annotated with NonConfigurationInstance when config changes occurs (screen rotation, locale change, etc) as the Fragment itself (only it's View hierarchy) is not destroyed if retained.
Examples speak better than words, so here's how it would look like:
@EBean
public class MyRecyclerView extends RecyclerView.Adapter {
@RootFragment
MyEFragment mFragment;
// I can now access the fragment, it's @InstanceState members,
// and keep easily my adapter state, such as selected items
}Below are the quotes to "help" implementation, from Gitter:
- @WonderCsabo: the problem is that it is not trivial to implement
- @LouisCAD: What would be the hard part(s)? Wouldn't it work by adding a setFragment_(MyEFragment fragment) call in onCreate() for all @ebeans in MyEFragment containing an @rootfragment field?
- @WonderCsabo: Yeah, it is not too hard, that was an overstatement.
I'm on the way to learn how Annotation Processors work under the hood, so I hope I'll be able to add this myself (If I begin working on this, I'll post updates here), but if in the meantime, someone with the skills wants to add this new feature, I'd be very grateful (and I hope I would not be the only one).