gpt4 book ai didi

Android fragment 管理器 fragment 变化监听器

转载 作者:太空狗 更新时间:2023-10-29 15:50:10 25 4
gpt4 key购买 nike

当使用 FragmentManager 替换 fragment 时,是否可以添加事件完成时的监听器?

所以

myFragmentManager
.beginTransaction()
.replace(R.id.fragmentContainer, fragment, FRAGMENT_TAG)
.commit()

我可以向 myFragmentManager 添加一个监听器以便我知道 fragment 何时被替换了吗?

最佳答案

FragmentManager (android.support.v4.app.FragmentManager) 有一个很好的方法叫做 registerFragmentLifecycleCallbacks,它可以帮助注册一个 FragmentManager.FragmentLifecycleCallbacks。这允许我们在 fragment 的生命周期内采取行动。您可以根据需要在 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();

关于Android fragment 管理器 fragment 变化监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50196455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com