I have a class ListFragment
public class ListFragment<T> extends DialogFragment {
when i try to cast an object to a specific type of ListFragment in this case a String
ListFragment<String> lfst = (ListFragment<String>)getFragmentManager().findFragmentByTag("somePicker");
It gives me a warning Type safety: Unchecked cast from Fragment to ListFragment
To remove the warning I have to add @SuppressWarnings("unchecked")
The reason I need ListFragment<String> as I will be calling a method that depends on the use of String.
So is there a way to achieve this without using a suppress warning. Note this code is being written for use on Android.
In a word, no. The
findFragmentByTagmethod doesn’t know that it will be returning aListFragmentand the superclass,Fragment, has no type parameters itself.The best that you can do is suppress the warning as you have done.