I notice that while using the new compileSdkVersion 26
casting is no longer needed in Fragments (android.support.v4.app.Fragment
). But I can't get the same method in AppCompatActivity
I have some preliminaries findings of the issue:
a. Inside a Fragment the new findViewById()
method is always used and correspond to this:
public <T extends android.view.View> T findViewById(int id) { /* compiled code */ }
b. Initially I though this will mean that has something to do then with the Activity, but using getActivity().findViewById()
does the same.
c. That leads me to think that the Activity inherits from another class, but when I trace upwards the getActivity()
and AppCompatActivity
I found both extends 'FragmentActivity'
d. While trying to use the findViewById()
method in an Activity both methods are suggested, but finally casting is always needed despite selected the desired one.
e. I have found a similar question here. There is stated that the new findViewById()
method belongs to View
so I try to found a View
inside a View
, something like this TextView tv = otherView.findViewById()
but still the casting is needed.
So, how can I use the new findViewById()
method in an Activity?