android - Activity Listener Implementation? -
i android/java novice , have question based on following scenario:
- i have baseactivity extended number of other activities, a1, a2, , a3
- activities a1, , a2 may request user enter part#s. activity a3 doesn't have functionality never ask part#.
- part# entered user in partdialogfragment. since user may prompted part# a1 , a2 activities, thinking put code instantiates , shows partdialogfragment in baseactivity don't have duplicate same call in activities a1 , a2.
- partdialogfragment has interface defines onpartentered()
question: should implement interface partdialogfragment in baseactivity or in activities a1 , a2?
basically, thinking this:
class partdialogfragment extends dialogfragment{ private partlistener mpartlistener; public interface partnumberlistener{ void onpartentered(string part#); } ... ... @override public void onattach(activity activity){ super.onattach(activity); try{ mpartlistener = (partlistener) activity; } catch(classcastexception e){ throw new classcastexception(activity.tostring() + " must implement partlistener"); } } ... ... } public abstract class baseactivity implements partdialogfragment.partnumberlistener { //**should implement here?** ... ... public void showpartnumberdialog(){ //creates , shows partdialogfragment defined above } } public class a1 extends activity implements partdialogfragment.partnumberlistener { //**or in a1 , a2?** ... ... if (something){ showpartnumberdialog(); //this defined in baseactivity } } public class a1 extends activity implements partdialogfragment.partnumberlistener { //**or in a1 , a2?** ... ... if (something){ showpartnumberdialog(); //this defined in baseactivity } } public class a3 extends activity{ ... ... //**this never ask part#, no need implement partdialogfragment.partnumberlistener here.** }
a1 , a2 activities need implement interface, because if base class abstract, definition required create subclasses of instantiate. subclasses required (by compiler) implement interface methods abstract class left out.
Comments
Post a Comment