gpt4 book ai didi

android - 重新加载 fragment 留下空白屏幕

转载 作者:行者123 更新时间:2023-11-29 02:23:26 25 4
gpt4 key购买 nike

我通过以下方式在 framelayout 中添加了 fragment

    mUserListFragment = new UserListFragment();
mMessageFragment = new MessageFragment();
mAccountFragment = new AccountFragment();

fragmentManager.beginTransaction().add(R.id.frame_layout, mAccountFragment, mAccountFragment.getClass().getSimpleName()).addToBackStack(mAccountFragment.getClass().getSimpleName()).commit();
fragmentManager.beginTransaction().add(R.id.frame_layout, mMessageFragment, mMessageFragment.getClass().getSimpleName()).addToBackStack(mMessageFragment.getClass().getSimpleName()).commit();
fragmentManager.beginTransaction().add(R.id.frame_layout, mUserListFragment, mUserListFragment.getClass().getSimpleName()).addToBackStack(mUserListFragment.getClass().getSimpleName()).commit();

后面我会按照下面的方式显示或者隐藏fragment

//for showing
fragmentManager.beginTransaction().show(fragment).commit();

//for hiding
fragmentManager.beginTransaction().hide(fragment).commit();

我从一个 fragment 转到另一个 Activity,通过一些 onClick 方法,该 Activity 重定向到保存 fragment 的同一个 Activity。当回到 fragment 持有者 Activity 时,我在 onResume 中重新加载 fragment ,但它使该 fragment 空白,但是通过调试我开始知道, fragment 是可见的并且 onCreateView 方法被调用,我尝试以下列方式重新加载 fragment

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

ft.setReorderingAllowed(false);

ft.detach(activeFragment).commitNow();
ft.attach(activeFragment).commitNow();

我也试过了

fragmentManager.beginTransaction().detach(activeFragment).attach(activeFragment).commitAllowingStateLoss();

所有这些只会留下空白屏幕。

最佳答案

您可以通过以下代码跟踪 fragment 的堆栈和 fragment 数。

getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {
public void onBackStackChanged() {
Log.i(TAG, "back stack changed ");
int backCount = getSupportFragmentManager().getBackStackEntryCount();

// you can also track which are current fragments are in stack by
getSupportFragmentManager().getFragments();

if(backCount ==0){

//then you have to check whats going wrong in your case
// suppose to kill current activity also by finish();
// I means its depending on your requirements.


}

}
});

它将帮助您找到自己的出路。

关于android - 重新加载 fragment 留下空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53670653/

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