I'm sorry, I can't seem to form a better sentence to ask this question. I'll try to clarify:
My class (currently an enum
, but porting it to a class
isn't an issue if it resolves this) has a Class field that denotes the type that it will dynamically instantiate at runtime.
This type must extend the Fragment
class (Android) and also implement MyInterface
.
<F extends Fragment & MyInterface>
How do I define this in code?
What works currently is an incomplete solution, which accepts any Fragment regardless of whether it implements MyInterface
:
public enum Something {
Value1 (OneFragmentInterfaceImplementor.class),
Value2 (AnotherFragmentInterfaceImplementor.class);
public final Class<? extends Fragment> fragment;
private Something (final Class<? extends Fragment> fragmentClass) {
fragment = fragmentClass;
}
}
What I really want, though is that this class not only extends Fragment
, but also implements MyInterface
.
I want the correct form of below:
public <F extends Fragment & MyInterface> enum Something {
Value1 (OneFragmentInterfaceImplementor.class),
Value2 (AnotherFragmentInterfaceImplementor.class);
public final Class<F> fragment;
private Something (final Class<F> fragmentClass) {
fragment = fragmentClass;
}
}
Am I asking for the impossible here?
Update A great suggestion was to create an abstract Fragment that implements the interface. Unfortunately, this won't work in my specific use-case because some of these fragments extend other already implemented fragments.