Reputation: 11329
I'm using Android Annotations in my Android Project. As implementing Parcelable is a lot of work I want to use Parceler and the @Parcel annotation.
The problem is that if I want to use the @FragmentArg annotation by Android Annotations it doesn't (for obvious reasons) recognize that the class will be generated with the Parcelable interface implemented. I now have two questions:
Until now my code for the Fragment looks like:
@EFragment(R.layout.fragment_faq)
public class FaqFragment extends ListFragment {
@FragmentArg
ArrayList<FaqItemImpl> faqItems;
// ...
}
The generated POJO class is annotated with @Parcel:
@Parcel
public class FaqItemImpl implements FaqItem {
protected String iconURL;
protected String title;
protected String question;
protected String answer;
protected ArrayList<FaqItemImpl> faqChildren;
// ...
}
In the generated FaqFragment_ the interesting part is:
// ...
public FaqFragment_.FragmentBuilder_ faqItems(ArrayList<FaqItemImpl> faqItems) {
args.putSerializable(FAQ_ITEMS_ARG, faqItems);
return this;
}
// ...
As you can see the generated class treads the POJO as Serializable...
Upvotes: 3
Views: 3531
Reputation: 11113
One approach you can use is to let AA handle the hand-off of the Parcelable and let Parceler perform the serialization/deserialization. One nice feature about Parceler is it will handle collection serialization for you, so AA should just have to deal with a single Parcelable. This would effectively avoid any reference to generated code, besides AA's underscore class of course.
Here's what I mean:
@EFragment(R.layout.fragment_faq)
public class FaqFragment extends ListFragment {
@FragmentArg
Parcelable faqParcelable;
public void useFaq(){
List<FaqItemImpl> faqItems = Parcels.unwrap(faqParcelable);
// ...
}
}
Then when you're ready to build FaqFragment, you would just have to have Parceler wrap your List:
FaqFragment_.builder()
.faqParcelable(Parcels.wrap(faqItems))
.build();
Yes, this approach is not as nice as AA making the wrap/unwrap call for you, but it should work.
Edit:
Working with the Android Annotation team we've added Parceler support to @Extra
, @FragmentArg
and @SavedInstanceState
annotated fields. This means the OP's desired functionality is in place. This should work:
@EFragment(R.layout.fragment_faq)
public class FaqFragment extends ListFragment {
@FragmentArg
ArrayList<FaqItemImpl> faqItems;
// ...
}
Upvotes: 3
Reputation: 12207
Unfortunetaly you cannot use @Parcelable
objects with @FragmentArg
(or other AA bundle injection annotations). Since you are using FaqItemImpl
which itself does not implement Parcelable
, AA cannot know how to deal with it. An (ugly) solution would be using the generated class:
@FragmentArg
ArrayList<FaqItemImpl.Parcelable> faqItems;
Actually there was an attempt to integrate parceler
into AndroidAnnotations
but was rejected due to some reasons.
There are plans to add Parcelable
boilerplate generator into AA directly, unfortunetaly it needs more initial work.
Upvotes: 0