fragmentargs
fragmentargs copied to clipboard
Generated code for Fragment with generic type gives compilation errors
I have a fragment with generic type. When I build the project, the generated *Builder
class contains Generic variable, but this variable is not declared in static new*Builder
method signature
For now, I'm manually creating the Fragment with bundle manually for this specific fragment I have. Can you get this issue fixed
PS: Please note that this issue exists with ParcellablePlease aswell if the you have a generic type
Generics are not supported yet. Can you please post the class definition of your Fragment so that I can see how you are applying the generics in combination with FragmentArgs. This would allow me to estimate how long does it will take to implement generics.
Can you also please post the code of the generated Builder class. Thanks!
Thanks for your prompt response
The fragment looks something like this
@FragmentWithArgs
public class CustomFragment<V extends Parcelable> extends Fragment {
@Arg
@State // from Icepick; Requires both ValueHolder & V to be Parcelable
ArrayList<ValueHolder<V>> choices;
// a lot more
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentArgs.inject(this);
}
// some more code here
}
The container object would look something like this (where I use ParcelablePlease
, which too have issues with generics)
@ParcelablePlease
public class ValueHolder<V extends Parcelable> implements Parcelable {
ArrayList<ValueHolder<V>> children;
V value;
// Generated parcelleable code
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
ValueHolderParcelablePlease.writeToParcel(this, dest, flags);
}
public static final Creator<ValueHolder> CREATOR = new Creator<ValueHolder>() {
public ValueHolder createFromParcel(Parcel source) {
ValueHolder target = new ValueHolder();
ValueHolderParcelablePlease.readFromParcel(target, source);
return target;
}
public ValueHolder[] newArray(int size) {
return new ValueHolder[size];
}
};
}
Please let me know if you need any additional info.
Thanks, yes generics are simply not implemented. Unfortunately I don't have much time for FragmentArgs nor ParcelablePlease at the moment. So don't expect that I will work in that before February.
However, there is a simple workaround: you can define your own ArgsBundler
(see readme). The same can be done with ParcelablePlease's Bagger
(see ParcelablePlease readme). Hence you don't have to setup your fragment's bundle manually, but you can define your own implementation how to put and read a certain type into/from fragment's bundle.
Sure. I'll use the workarounds for now.
Thanks
Do you have any example of a custom ArgsBundler
handling generics? I'm not sure how to do it. I tried something like this
public class ConfigurableMetadataArgsBundler<CM extends ConfigurableMetadata> implements ArgsBundler<CM> {
@Override
public void put(String key, CM value, Bundle bundle) {
bundle.putParcelable(key, value);
}
@Override
public <V extends CM> V get(String key, Bundle bundle) {
return bundle.getParcelable(key);
}
}
Where my Fragment
looks like this:
public abstract class ConfigurableDataDetailFragment<CM extends ConfigurableMetadata>
extends BaseFragment {
@Arg (bundler = ConfigurableMetadataArgsBundler.class)
public CM configurableMetadata;
...
}
But I still got the same issue as when I didn't had any ArgsBundler
.
Thank's.