android - Call Fragment B from Fragment A using ViewPager Tabs -


i have done many programs, have implemented multiple fragments inside single activity, not when using single activity host multiple fragments tabs , on tap show fragments...

using materialviewpager library, in calling different different fragments show views in respective tabs.

like first tab, using 2 fragments, where

in first fragment, using recyclerview... show list of menus.

and in second fragment, using recyclerview... show list of items under particular menu.

so here question how call fragment fragment.

mrecyclerview.addonitemtouchlistener(new recycleritemclicklistener(getactivity(), mrecyclerview ,new recycleritemclicklistener.onitemclicklistener() {             @override             public void onitemclick(view view, int position) {                  value value = valuelist.get(position);                 list<learning> learning = value.getlearning();                  // using when putting "item" data same recyclerview                 // on press exiting, not showing list of menus again                 /**                 learningadapter = new learningadapter(learning, r.layout.card_learning, getactivity());                 mrecyclerview.setadapter(learningadapter);                 **/                  itemfragment fragment = new itemfragment();                 replacefragment(fragment);              } 

method replacefragment

public void replacefragment(fragment somefragment) {     fragmenttransaction transaction = getfragmentmanager().begintransaction();     // using fragment not activity, can use frame_container in xml     transaction.replace(r.id.frame_container, somefragment);      transaction.addtobackstack(null);     transaction.commit(); } 

you can have callback interface implemented activity hosts these 2 fragments. fragment use call notify activity replace fragment b. depending on need, can pass parameters across through callback method itself.

your callback interface:

public interface yourcallback {     public void methodwhichreplacesfragmentawithb(params...); } 

your activity hosting fragments:

public class youractivity extends ... implements yourcallback {   ..   ..   @override   public void methodwhichreplacesfragmentawithb(params...) {     //insert replace fragment code here   } }   

your fragment have callback object, yourcallback callbackobj;. callback object can initialised using activity (pass this activity) since activity has implementation of interface. now, can use

callbackobj.methodwhichreplacesfragmentawithb(actual_params...);

to replace fragment. callback interface can exploited other communications parent activity other fragment hosted in activity.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -