java - Main Activity get data from 2 fragments -


i have read through posts on here regarding activity-fragment communication problem unique; try state clear possible understand. in app there mainactivity , 2 fragments(we call these fragment1 , fragment2) in sliding tab layout. main activity contains navigation drawer, , fragment1 contains textview , other fragment2 contains edittext. heres problem now, there option on drawer called share:- when clicked, want access string value in textview of fragment1 or value in edittext of fragment2; depends on fragment active in tablayout. want pass string value text message argument of intent in order shared whatever client user chooses.

@override public boolean onnavigationitemselected(menuitem item) {     // handle navigation view item clicks here.     int id = item.getitemid();      if (id == r.id.nav_share) {       //inside onnavigationitemselected         string value = " ";         fragment currentfragment= getactivefragment();          if(currentfragment instanceof speechtotext){             value = ((speechtotext)currentfragment).gettext1();         }else if(currentfragment instanceof tts){             //this 1 has edittext             value = ((tts)currentfragment).gettext2();         } //then create intent //intent shareintent ...         intent shareintent = new intent(intent.action_send);         shareintent.settype("text/plain");         shareintent.putextra(intent.extra_text,value);         shareintent.putextra(intent.extra_subject, "message");         startactivity(intent.createchooser(shareintent, "share via"));     } 

i have function post provided return current active fragment in tab layout.

    public fragment getactivefragment() {     if (getsupportfragmentmanager().getbackstackentrycount() == 0) {         return null;     } string tag =    getsupportfragmentmanager().getbackstackentryat(getsupportfragmentmanager().getbackstackentrycount() - 1).getname();     return getsupportfragmentmanager().findfragmentbytag(tag); } 

i have implementation methodforgettingtextviewvalue() in speechtotext fragment fragment1: public string gettext1() { return resulttext.gettext().tostring(); }

i have implementation methodforgettingedittextvalue() in tts fragment fragment2 public string gettext2() { return return edittext.gettext().tostring(); }

you need keep track of active fragment in tab layout, check class of fragment, , depending on 1 is, call method return either content of textview or of edittext.

to keep track of current fragment, check out question.

once have part sorted, inside onnavigationitemselected implementation, before create intent you'd this, assuming currentfragment being updated, , you've implement needed methods in each of 2 fragments:

//inside onnavigationitemselected string value = ""; if(currentfragment instanceof mycustomfragment1){     value = ((mycustomfragment1)currentfragment).methodforgettingtextviewvalue(); }else if(currentfragment instanceof mycustomfragment2){     //this 1 has edittext     value = ((mycustomfragment2)currentfragment).methodforgettingedittextvalue(); } //then create intent //intent shareintent ... 

in case it's not clear enough, let me insist you'd need implement methodforgettingedittextvalue , methodforgettingtextviewvalue. method should access view id, value , return it.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -