使用f^(-1)(f(x)) = x
替换片段时,是否可以在事件完成时添加监听器?
所以
FragmentManager
我可以向myFragmentManager
.beginTransaction()
.replace(R.id.fragmentContainer, fragment, FRAGMENT_TAG)
.commit()
添加一个监听器,以便我知道片段何时被替换?
答案 0 :(得分:4)
FragmentManager (android.support.v4.app.FragmentManager)
有一个名为registerFragmentLifecycleCallbacks
的好方法,它有助于注册FragmentManager.FragmentLifecycleCallbacks
。这允许我们在片段的生命周期中采取行动。您可以根据需要在onFragmentCreated()
中执行某些操作。
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();
答案 1 :(得分:0)
当新的Fragment
处于活动状态时,系统会启动新的onResume
Fragment
功能。当您在其中替换上一个Fragment
时,您可以执行您想要执行的操作。
答案 2 :(得分:0)
您可以在活动类中实现以下侦听器,以监视对碎片堆栈的更改。
public static interface FragmentManager.OnBackStackChangedListener
稍后您需要在
的帮助下将此侦听器注册到片段管理器 addOnBackStackChangedListener (FragmentManager.OnBackStackChangedListener listener)
供您参考的链接