I have problem in replacing fragments in android.
Both of my fragment layouts are placed in a FrameLayout.
And I have tried replacing two fragment using this code :
Fragment newFragment = new StatisticsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(((ViewGroup)(getView().getParent())).getId(), newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
The problem is that after replacing the Fragment, its layout is not shown although its OncreateView
is called.
Please help me.
EDIT : I put the hole code.
I solved my problem by adding background color to my layouts.
答案 0 :(得分:1)
This is how I replace frameLayouts with fragments:
fm = getSupportFragmentManager();
Fragment_Main main = new Fragment_Main();
fm.beginTransaction().replace(R.id.mainContainer, main).commit();
Try adding .commit
before ending the statement
答案 1 :(得分:0)
This method allows you to load any Fragment to any FrameLayout:
/**
* Function to load a fragment into a inner FrameLayout of auction precap view
* @param Fragment to load
* @param If of the container to load fragments to
*/
private void loadInnerFragment(Fragment fragment, int containerId) {
try {
FragmentManager childFragmentManager = getChildFragmentManager();
FragmentTransaction transaction2 = childFragmentManager.beginTransaction();
transaction2.replace(containerId, fragment);
transaction2.commit();
}
catch (Exception e) {
e.printStackTrace();
}
}
Thus, just do:
Fragment newFragment = new StatisticsFragment();
loadInnerFragment(
newFragment,
R.id.container
);
Where R.id.container
is the ID of your FrameLayout