gpt4 book ai didi

android - 即使 fragment 被隐藏,FragmentTabHost 也会触发 onCreateView

转载 作者:行者123 更新时间:2023-11-30 03:28:24 25 4
gpt4 key购买 nike

我在 fragment 中有一个 FragmentTabHost。我有 4 个标签。当我点击标签时,即使我使用 fragment 事务隐藏它,标签的 onCreateView 也会被触发?

我调试发现,当我从home选项卡中点击create选项卡时

CreateFragment createFragment = (CreateFragment) fm.findFragmentByTag(CREATE_TAG);

即使我在 tabHost.addTab 中添加了它,它还是 null?

如何在选项卡之间移动而不调用每个 fragment 的 onCreateView?

谢谢

这是 fragment 类

public class TabFragment extends Fragment {

public static final String HOME_TAG = "home";
public static final String CREATE_TAG = "create";
public static final String SEARCH_TAG = "search";
public static final String PROFILE_TAG = "profile";


private MyFragmentTabHost tabHost;
private MyFragmentTabHost.TabInfo tabInfo;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_tab, viewGroup, false);

tabHost = (MyFragmentTabHost) view.findViewById(android.R.id.tabhost);
tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec(HOME_TAG).setIndicator(getString(R.string.tab_home)), HomeFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(CREATE_TAG).setIndicator(getString(R.string.tab_create)), CreateFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(SEARCH_TAG).setIndicator(getString(R.string.tab_search)), CreateFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(PROFILE_TAG).setIndicator(getString(R.string.tab_profile)), CreateFragment.class, null);

MyFragmentTabHost.OnTabChangeListener tabListener = new MyFragmentTabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String s) {
FragmentManager fm = getChildFragmentManager();
HomeFragment homeFragment = (HomeFragment) fm.findFragmentByTag(HOME_TAG);
CreateFragment createFragment = (CreateFragment) fm.findFragmentByTag(CREATE_TAG);

FragmentTransaction ft = fm.beginTransaction();

if(s.equalsIgnoreCase(HOME_TAG)){
if(homeFragment == null)
ft.add(R.id.tabcontent, new HomeFragment(), HOME_TAG);
else{
if(createFragment != null)
ft.hide(createFragment);
ft.show(homeFragment);
}
}
else if(s.equalsIgnoreCase(CREATE_TAG)){
if(createFragment == null)
ft.add(R.id.tabcontent, new CreateFragment(), CREATE_TAG);
else{
if(homeFragment != null)
ft.hide(homeFragment);
ft.show(createFragment);
}
}

ft.commit();
}
};

tabHost.setOnTabChangedListener(tabListener);

return view;
}
}

最佳答案

在 FragmentTabHost 中:

private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
TabInfo newTab = null;
for (int i=0; i<mTabs.size(); i++) {
TabInfo tab = mTabs.get(i);
if (tab.tag.equals(tabId)) {
newTab = tab;
}
}
if (newTab == null) {
throw new IllegalStateException("No tab known for tag " + tabId);
}
if (mLastTab != newTab) {
if (ft == null) {
ft = mFragmentManager.beginTransaction();
}
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(mContext,
newTab.clss.getName(), newTab.args);
ft.add(mContainerId, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}

mLastTab = newTab;
}
return ft;
}

Transaction.attach(Fragment) 将重新创建 View ,请参阅源代码中的详细信息。所以,你可以在你的 fragment 中添加这样的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (!isCreated || mRootView == null) {
isCreated = true;
mRootView = inflater.inflate(R.layout.fragment_home_task, container, false);
ButterKnife.bind(this,mRootView);
onViewInitialized();
}
return mRootView;
}

关于android - 即使 fragment 被隐藏,FragmentTabHost 也会触发 onCreateView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17860867/

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