android - How to pass data between fragments of a FragmentTabHost? -


i'm trying learn android , having issue project. have set fragment tab host 4 fragments. have first 3 fragments interactive user , when tab on 4th tab, display information put in on first three. figured can pass info overriding on onpause() trigger don't want use button press. right now, i'm trying edittext work make sure i'm doing right. i'm not sure if i'm using fragment transaction correctly, or way i'm trying collect edit text field. later on hope pass information via bundle. appreciated.

main:

package valdes.fragmenttabsmenu;  import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmenttabhost; import android.app.fragmenttransaction;  public class mainactivity extends fragmentactivity implements welcomefragment.welcomelistener {  private fragmenttabhost mtabhost; private string firstname;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     mtabhost = (fragmenttabhost) findviewbyid(android.r.id.tabhost);     mtabhost.setup(this, getsupportfragmentmanager(), android.r.id.tabcontent);     mtabhost.addtab(mtabhost.newtabspec("home").setindicator("home", null), welcomefragment.class, null);     mtabhost.addtab(mtabhost.newtabspec("demographics").setindicator("demographics", null), demographicsfragment.class, null);     mtabhost.addtab(mtabhost.newtabspec("questions").setindicator("questions", null), questionsfragment.class, null);     mtabhost.addtab(mtabhost.newtabspec("overview").setindicator("overview", null), overviewfragment.class, null); }   @override public void getfristname(string first_name){     firstname = first_name;     overviewfragment fragment = new overviewfragment();     fragmenttransaction ft = getfragmentmanager().begintransaction();     fragment.setfirstname(firstname);     ft.addtobackstack(null);     ft.commit();  } } 

fragment 1 - getting info

package valdes.fragmenttabsmenu;   import android.content.context; import android.os.bundle; import android.support.v4.app.fragment; import android.text.textwatcher; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.edittext; import android.widget.imageview; import android.widget.textview;   public class welcomefragment extends fragment {   public welcomefragment() {     // required empty public constructor }  interface welcomelistener{     public void getfristname(string firstname);  }  private welcomelistener listener; private string firstname;  @override public void oncreate(bundle savedinstancestate){     super.oncreate(savedinstancestate); }   @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     view view = inflater.inflate(r.layout.fragment_welcome, container, false);     edittext edittext = (edittext) view.findviewbyid(r.id.first_name);     firstname = edittext.gettext().tostring();     return view; }  @override public void onattach(context context){     super.onattach(context);     this.listener = (welcomelistener)context; }    @override public void onpause(){     super.onpause();    if(listener != null){        listener.getfristname(firstname);    } } } 

fragment 1 xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="@dimen/activity_horizontal_margin">  <textview     android:id="@+id/welcome"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparenttop="true"     android:layout_centerhorizontal="true"     android:layout_gravity="center_horizontal"     android:textsize="25sp"     android:text="@string/welcome_message"/>   <edittext     android:id="@+id/first_name"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:hint="@string/first_name"/>  <edittext     android:id="@+id/last_name"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:hint="@string/last_name"/>  <spinner     android:id="@+id/birthday"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:entries="@array/months"/>   </linearlayout> 

fragment 2 - getting info

package valdes.fragmenttabsmenu;   import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview;   public class overviewfragment extends fragment {  private string firstname; private string lastname;   public overviewfragment() {     // required empty public constructor }   @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     return inflater.inflate(r.layout.fragment_overview, container, false); }  @override public void onstart(){     super.onstart();     view view = getview();     if(view != null){         textview name = (textview) view.findviewbyid(r.id.ov_name);         name.settext(firstname);      } }  public void setfirstname(string firstname){this.firstname = firstname;}  } 

fragment 2 xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="valdes.fragmenttabsmenu.overviewfragment">  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparenttop="true"     android:layout_centerhorizontal="true"     android:layout_gravity="center_horizontal"     android:textsize="25sp"     android:layout_weight="1"     android:text="@string/overview"/>  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_weight="1"     android:layout_gravity="center_horizontal"     android:textsize="40sp"     android:text="responses go here"/>  <textview     android:id="@+id/ov_name"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:textsize="20sp"     android:text="test"/>  <button     android:id="@+id/submit_button"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="@string/submit"/>  </linearlayout> 

create listner get/set data between fragments , implement on activity this

interactionlister.java

public interface interactionlister {      void setdata(string data);      string getdata();  } 

mainactivity.java

public class tabactivity extends activity implements interactionlister{      private string mdata;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);     }      @override     public void setdata(string data) {         this.mdata = data;     }      @override     public string getdata() {         return mdata;     } } 

tabfragment1.java

public class tabfragment1 extends fragment {     interactionlister interactionlister;     private static final string tag = tabfragment1.class.getsimplename();      @override     public void onviewcreated(view view, bundle savedinstancestate) {         log.w(tag,"data other fragment " + interactionlister.getdata());      }      @override     public void onattach(activity activity) {         super.onattach(activity);         interactionlister = (interactionlister) activity;     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -