I'm trying to understand the best way to organize the two classes below. The sample code, I'm working with, had the DetailFragment as a nested class within ListViewTest. I would like to move DetailFragment into it's own class allowing me to init from any object. The only problem I see is the cast on line 19: ((ListViewTest) getActivity()).doPositiveClick();
What if I wanted to use DetailFragment with a class called ListViewNew? How would I know which class to cast to?
You do it the same way you would if you had multiple implementations in normal classes. You create an interface which contains the method(s) you want to call from the activity. Then you have the activities implement the interface. In the Fragment you cast to the interface, not the activity implementation.
Joined: Jul 18, 2013
Thank you, this explanation really helped me understand interfaces too!