android - One Interface for Multiple Fragments -


can please tell me if i'm solving correctly or if should go route?

this simplified example: have 1 activity , 2 fragments. each fragment has button when clicked, relays click activity , toast pops within activity.

i know fragment communicates activity through interface. if have multiple fragments have similar interface. example, here both fragments use onclick type of interface communicate activity

 static interface onclickedlistener{     public void buttonclicked(view v); } 

is better to

a) create separate interface class , attach within both fragments. example fragment 1:

public class fragment1 extends fragment implements onclickedlistener{   private onclickedlistener clickedinterface;  public fragment1() {     // required empty public constructor }  @override public void buttonclicked(view v) { }  @override public void onattach(activity activity) {     super.onattach(activity);     this.clickedinterface = (onclickedlistener)activity; }} 

fragment 2:

public class fragment2 extends fragment implements onclickedlistener{  private onclickedlistener clickedinterface;  public fragment2() {     // required empty public constructor }  @override public void buttonclicked(view v) { }  @override public void onattach(activity activity) {     super.onattach(activity);     this.clickedinterface = (onclickedlistener)activity; } 

or

b) create individual interfaces unique specific fragment , implement in mainactivity instead of 1 interface mentioned above. thank you.

first create custom fragment in implement interface.

    public class customfragment extends fragment implements onclickedlistener{         public onclickedlistener clickedinterface;          @override         public void buttonclicked(view v) {         }          @override         public void onattach(activity activity) {              super.onattach(activity);              this.clickedinterface = (onclickedlistener)activity;         } } 

now, can add in every fragment

(i) fragment 1

public class fragment1 extends customfragment {     ...... } 

(ii) fragment 2

public class fragment2 extends customfragment {     ...... } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -