Android Replace the Current Fragment with Another Fragment

Replace Current fragment with another fragment from within the Current fragment

I see BenefitFragment will be your parent Fragment for both ConfirmPinFragment and CorporateFragment.

So pass R.id.benefitContainer as a container of your fragment in which your fragment to be replaced. But you need to resolve that id first. to do so you need to use getParentFragment().

Try to write this in your ConfirmPinFragment.

Fragment mF = getParentFragment();
// double check
if (mF instanceof BenefitFragment) {
getFragmentManager().beginTransaction()
.add(((BenefitFragment)getParentFragment()).getView().findViewById(R.id.benefitContainer).getId()
, new CorporateFragment())
.setCustomAnimations(R.anim.slide_in_left, R.anim.do_nothing)
.addToBackStack("benefit")
.commit();
}

Android replace the current fragment with another fragment

Then provided your button is showing and the click event is being fired you can call the following in your click event:

final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();

and if you want to go back to the DetailsFragment on clicking back ensure you add the above transaction to the back stack, i.e.

ft.addToBackStack(null);

Or am I missing something? Alternatively some people suggest that your activity gets the click event for the button and it has responsibility for replacing the fragments in your details pane.

How to replace fragment with another fragment?

As @RishabhDuttSharma suggested, Use a FrameLayout instead of fragment in your activity layout and initialize the first fragment in code.

Layout:

<FrameLayout
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Code:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Fragment fr = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}

Replace fragment by another using default navigation drawer activity template

If you are using a NavHostFragment and the Navigation Component, then you never use a FragmentTransaction. Instead you navigate to a screen by using the APIs on NavController.

How to replace the activity's fragment from the fragment itself?

It's actually easy to call the activity to replace the fragment.

You need to cast getActivity():

((MyActivity) getActivity())

Then you can call methods from MyActivity, for example:

((MyActivity) getActivity()).replaceFragments(Object... params);

Of course, this assumes you have a replaceFragments() method in your activity that handles the fragment replace process.

Edit: @ismailarilik added the possible code of replaceFragments in this code with the first comment below which was written by @silva96:

The code of replaceFragments could be:

public void replaceFragments(Class fragmentClass) {
Fragment fragment = null;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment)
.commit();
}


Related Topics



Leave a reply



Submit