android - What is the analog of startActivityForResult() but using fragments -


i have 4 tabs, each runs fragment when selected.
tab #2 fragment want start fragment run on tab #2 , results it. right way this? there examples around?

if activities, , not fragments, know use startactivityforresult().

there's recommended pattern fragment interaction. each of fragments declares way needs interact others using interface:

here's code first fragment:

public class fragment1 {     private listener listener;      @override     public void oncreateview(...) {         bundle args=getarguments();                     if (args!=null) {             object arg=args.getparcelable("result");             //use result         }          //...         someview.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 if (listener!=null)                     listener.onfragment2requested();             }         });         //...     }      @override     public void onattach(activity activity) {         super.onattach(activity);          this.listener=(listener) activity;     }      public interface listener {         public void onfragment2requested();     } } 

and second fragment:

public class fragment2 {     private listener listener;      @override     public void oncreateview(...) {         //...         someview.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 object object=...                  if (listener!=null)                     listener.onworkdone(object);             }         });         //...     }      @override     public void onattach(activity activity) {         super.onattach(activity);          //will throw classcastexception unless activity         //implements listener interface of fragment         this.listener=(listener) activity;     }      public interface listener {         public void onworkdone(object someresult);         public void someothermethod();     } } 

and then, finally, activity, acts coordinator:

public class youractivity extends                                fragmentactivity                            implements                                fragment1.listener,                                fragment2.listener {      public void onfragment2requested() {         fragment2 frg2=new fragment2();         //here goes code switch fragments way want         }      public void onworkdone(object someresult) {         //if don't want create new instance of fragment each time         //you can try fragmentmanager.         //but don't try store reference anywhere (activity field etc.)         //it interfere fragment recycling mechanism         fragment1 frg1=new fragment1();         bundle args=new bundle();         args.putparcelable("result", someresult);         //instead of arguments, use setter method, way          //your object lost when fragment recreated upon orientation change, etc.         frg1.setarguments(args);         //here goes code switch fragments way want     }      public void someothermethod() {         //...     } } 

i know, might seem of code, way it's done. also, pattern covered in article.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -