Call Fragment B From Fragment A Using Viewpager Tabs
Solution 1:
you can have a callback interface that is implemented by the activity which hosts these two fragments. Fragment A will use the call back to notify the activity to replace A with fragment B. Depending on your need, you can pass parameters across through the callback method itself.
Your callback interface:
publicinterfaceYourCallback {
publicvoidmethodWhichReplacesFragmentAwithB(params...);
}
Your Activity hosting fragments:
publicclassYourActivityextends ... implementsYourCallback {
..
..
@OverridepublicvoidmethodWhichReplacesFragmentAwithB(params...) {
//insert replace fragment code here
}
}
Your fragment will have a callback object, YourCallback callbackObj;
. This callback object can be initialised using the activity (pass as this
from activity) itself since the activity has the implementation of the interface. Now, you can use
callbackObj.methodWhichReplacesFragmentAwithB(actual_params...);
to replace the fragment. This callback interface can be exploited for other communications to parent Activity as well as other fragment hosted in that activity.
Solution 2:
To replace fragment,
FragmentTransactiontransaction= getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, someFragment).addToBackStack("null").commit();
Solution 3:
You can try this:
YourFragmentfragment=newYourFragment();
FragmentTransactiontransaction= ((AppCompatActivity) mContext).getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
Solution 4:
There is One option which i have been using from long time before. I will added the solution with clear example.
So you want to pass the value from fragment A
to fragment B
So, here what you want to do is, you have pass the value through the activity.
1) you pass the value from fragment A
to activity
2) then activity
to fragment B
Step 1: first create an Interface Like below in fragment A
publicinterfaceHomePage {
voidonHomeButtonsClicked(String clickedButton);
}
publicvoidsetOnHomeButtonClicked(HomePage homePage) {
this.homePage = homePage;
}
Step 2: then create the instance like below in Activity. where fragment created in PagerAdapter
((FragmentA) fragment).setOnHomeButtonClicked(newFragmentA.HomePage() {
@OverridepublicvoidonHomeButtonsClicked(String buttonClicked) {
pageSelected.onHomeButtonsClicked(selectedPage);
}
}
});
Step 3: then create the interface in Activity like below.
publicinterfacePageSelected {
voidonHomeButtonsClicked(String clickedButton);
}
publicvoidsetOnHomeButtonClicked(PageSelected pageSelected) {
this.pageSelected = pageSelected;
}
step 3: then add the following method in fragment B
in onCreateView or onResume()
((MainActivity) getActivity()).setOnHomeButtonClicked(newMainActivity.PageSelected() {
@OverridepublicvoidonHomeButtonsClicked(String clickedButton) {
//fragment B received the value here
}
});
Step 5: finally add the line in fragment A
in button click or where you want to pass the value.
homePage.onHomeButtonsClicked("Some String , by this code it is in String. you can change in your own data type");
You can rise your question if you face any difficulties.
Post a Comment for "Call Fragment B From Fragment A Using Viewpager Tabs"