android - How to pass data from one swipe tab to another? -


i need pass 2 string first tab second tab. although wrote code:

 map map = new map();             bundle bundle = new bundle();             bundle.putstring("position", intent.getstringextra("position"));             map.setarguments(bundle);             toast.maketext(context,"visualizzare la posizione sulla mappa",toast.length_short).show(); 

in first class, , code:

 bundle args = getarguments();      if(args != null)     {      toast.maketext(getactivity().getbasecontext(),"non รจ null",toast.length_short).show();      string pos=args.getstring("position");      string id=args.getstring("id");     } 

in second class, app crashed. don't understand why. searched lot found reason don't understand how can do. tell me how pass 2 strings first fragment second fragment? lot

for communicating between fragments must follow process : - must define interface communicator between fragment , parent activity , pass data set arguments in bundle .

to define interface communicator between fragments: http://developer.android.com/training/basics/fragments/communicating.html

to set arguments :

fragment fragment = new fragment();  final bundle bundle = new bundle();  bundle.putstring("data", data);  log.i("bundle", bundle.tostring());  fragment.setarguments(bundle);  

here process : fragmenta --data--> fragmentactivity --data--> fragmentb

1 - define interface communicator callback fragment

interface communicate {   public void senddata(string data); }  

2 - in parent activity implement communiactor

import android.os.bundle;  import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentactivity; import android.view.menu;  public class mainactivity extends fragmentactivity implements communicate{      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public void senddata(string data) {         // todo auto-generated method stub      // fragments can call method of reference of communicate.      }  } 

3 - in first fragment child ,we must instance of communiactor on activity created :

public class fragmenta extends fragment implements onclicklistener{  button button;  communicate cm;     @override     public view oncreateview(layoutinflater inflater, viewgroup container,              bundle savedinstancestate) {         // todo auto-generated method stub          return inflater.inflate(r.layout.layout_fragmenta, container, false);     }        @override     public void onactivitycreated(bundle savedinstancestate) {         // todo auto-generated method stub         super.onactivitycreated(savedinstancestate);         **cm = (communicate) getactivity();**         button = (button) getactivity().findviewbyid(r.id.button1);         button.setonclicklistener(this);     }        @override     public void onclick(view arg0) {         // todo auto-generated method stub          cm.senddata();     }  } 

4 - in parent activity data methode senddata , pass data fragmentb via bundle :

@override         public void senddata(string data) {             // todo auto-generated method stub        fragment fragment = new fragment();      final bundle bundle = new bundle();      bundle.putstring("data", data);      log.i("bundle", bundle.tostring());      fragment.setarguments(bundle);          } 

5 - ; data fragmentb arguments methods :

public class examplefragment extends fragment {

public static fragmentb newinstance() {     fragmentb fragment = new fragmentb();      // arguments     bundle arguments = new bundle();     arguments.putstring(argument_product_id, productid);     fragment.setarguments(arguments);      return fragment; }  public fragmentb() {}  @override public void oncreate(bundle savedinstancestate)  {     super.oncreate(savedinstancestate);      // handle fragment arguments     bundle arguments = getarguments();     if(arguments != null)     {     }  } 

}


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -