When replacing a fragment using a FragmentManager
is it possible to add a listener for when the event is completed?
So
myFragmentManager
.beginTransaction()
.replace(R.id.fragmentContainer, fragment, FRAGMENT_TAG)
.commit()
can I add a listener to myFragmentManager
so that I know when the fragment has been replaced?
FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.
The title of the toolbar should change between the two fragments (from "First Fragment" to "Second Fragment"). The second fragment should have a "back button" (arrow left icon) in the toolbar that, on click, lets the user go back to the first fragment.
We can communicate within fragments using the ViewModel. We can also communicate between fragments using Interface.
FragmentManager (android.support.v4.app.FragmentManager)
has a nice method called registerFragmentLifecycleCallbacks
which helps register a FragmentManager.FragmentLifecycleCallbacks
. This allows us to take actions during the fragment's life cycle. You can do something in onFragmentCreated()
as you wish.
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentPreAttached(FragmentManager fm, Fragment f, Context context) {
super.onFragmentPreAttached(fm, f, context);
Log.v("FragXX1", f.getTag());
}
@Override
public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {
super.onFragmentAttached(fm, f, context);
Log.v("FragXX2", f.getTag());
}
@Override
public void onFragmentCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {
super.onFragmentCreated(fm, f, savedInstanceState);
Log.v("FragXX3", f.getTag());
}
@Override
public void onFragmentActivityCreated(FragmentManager fm, Fragment f, Bundle savedInstanceState) {
super.onFragmentActivityCreated(fm, f, savedInstanceState);
Log.v("FragXX4", f.getTag());
}
@Override
public void onFragmentViewCreated(FragmentManager fm, Fragment f, View v, Bundle savedInstanceState) {
super.onFragmentViewCreated(fm, f, v, savedInstanceState);
Log.v("FragXX5", f.getTag());
}
@Override
public void onFragmentStarted(FragmentManager fm, Fragment f) {
super.onFragmentStarted(fm, f);
Log.v("FragXX6", f.getTag());
}
@Override
public void onFragmentResumed(FragmentManager fm, Fragment f) {
super.onFragmentResumed(fm, f);
Log.v("FragXX7", f.getTag());
}
@Override
public void onFragmentPaused(FragmentManager fm, Fragment f) {
super.onFragmentPaused(fm, f);
Log.v("FragXX8", f.getTag());
}
@Override
public void onFragmentStopped(FragmentManager fm, Fragment f) {
super.onFragmentStopped(fm, f);
Log.v("FragXX9", f.getTag());
}
@Override
public void onFragmentSaveInstanceState(FragmentManager fm, Fragment f, Bundle outState) {
super.onFragmentSaveInstanceState(fm, f, outState);
Log.v("FragXX10", f.getTag());
}
@Override
public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {
super.onFragmentViewDestroyed(fm, f);
Log.v("FragXX11", f.getTag());
}
@Override
public void onFragmentDestroyed(FragmentManager fm, Fragment f) {
super.onFragmentDestroyed(fm, f);
Log.v("FragXX12", f.getTag());
}
@Override
public void onFragmentDetached(FragmentManager fm, Fragment f) {
super.onFragmentDetached(fm, f);
Log.v("FragXX13", f.getTag());
}
}, true);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, new TradeFragment(), "trien");
ft.commit();
Your new Fragment
's onResume
function is launched when the new Fragment
is active. You can do what you want to do when the previous Fragment
is replaced in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With