java - Activity Class Downcasting -
I can have 2 different activities that can "parse" the same piece. Inside the piece I need to save a reference to the container activity.
MyActivity1 extends FragmentActivity MyActivity2 extends FragmentActivity
The reference is stored within one piece square area. I can not be normal:
personal activity activity;
And then:
activity = getActivity ();
This way I can not use the methods applicable to custom MyActivity1 / MyActivity2, Downcasting does not appear to help as well:
If (getActivity () MyActivity1 instanceof) {activity = (MyActivity1) getActivity (); Activity.mMethod1 (); // NOPE} and {activity = (MyActivity2) getActivity (); Activity.mMethod2 (); // NOPE}
Should I use two fields (one per activity type) and leave one blank? Is there any Java smart way to do this? Hope that my question is clear.
This does not help because you are not telling specific types of castings for specific I suggest you to have an abstract class with an abstract method, and two solid extensions of this class, to provide your own implementation of that abstract method. In this case you can keep the context of the intangible class, and the method call will be sent to the correct concrete instance
if getActivity () MyActivity1 instanceof) {(MyActivity1) getActivity ()) mMethod1 () .; } And if (getActivity) (for example MyActivity2) {(MyActivity2) getActivity ()). MMethod2 (); }
Comments
Post a Comment