I have 2 different activities that can "instantiate" the same fragment. Inside the fragment I need to save a reference to the container activity.
MyActivity1 extends FragmentActivity
MyActivity2 extends FragmentActivity
The reference is stored inside a Fragment class field. I cannot be generic:
private Activity activity;
and then:
activity = getActivity();
This way I cannot use methods implemented for custom MyActivity1/MyActivity2. Downcasting doesn't seem to help as well:
if (getActivity() instanceof MyActivity1) {
activity = (MyActivity1) getActivity();
activity.mMethod1(); // NOPE
} else {
activity = (MyActivity2) getActivity();
activity.mMethod2(); // NOPE
}
Should I use two field (one per activity type) and leave one blank? Any java smarter way to do this? Hope my question is clear.