重写FragmentTabHost,防止FragmentTabHost切换fragment重新调用

可以发现,在上一篇中的Demo中,每次FragmentTabHost切换fragment时会调用onCreateView()重绘UI。

简单改造一下FragmentMy代码(FragmentHome和FragmentMessage不给出),每次切换时总会有Toast。

public class FragmentMy extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_my, null);T.showShort(getActivity(), "FragmentMy==onCreateView");return view;}}为了防止FragmentTabHost切换fragment重新调用onCreateView(),可以重写FragmentTabHost

FragmentTransaction有几个常用方法:

attach(Fragment fragment)

Re-attach a fragment after it had previously been deatched from the UI with detach(Fragment).This causes its view hierarchy to be re-created, attached to the UI, and displayed.

detach(Fragment fragment)

DDetach the given fragment from the UI. This is the same state as when it is put on the back stack:the fragment is removed from the UI, however its state is still being actively managed by the fragment manager.When going into this state its view hierarchy is destroyed.(从UI中移除)

hide(Fragment fragment)

Hides an existing fragment.This is only relevant for fragments whose views have been added to a container,as this will cause the view to be hidden.(只是隐藏,不会销毁)

show(Fragment fragment)

Shows a previously hidden fragment.This is only relevant for fragments whose views have been added to a container, as this will cause the view to be shown.(把隐藏的显示出来)

所以只需重写FragmentTabHost,将detach替换为hide,隐藏Fragment,将attach替换为show,显示Fragment就可以了。

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) {// 将detach替换为hide,隐藏Fragment// ft.detach(mLastTab.fragment);ft.hide(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 {// 将attach替换为show,显示Fragment// ft.attach(newTab.fragment);ft.show(newTab.fragment);}}mLastTab = newTab;}return ft;}注意在XML中更改命名空间

Demo下载地址:

还有一种在fragment onCreateView 里缓存View,,防止每次onCreateView 的时候重绘View

以下代码来自网上:

public class FragmentHome extends Fragment {private View rootView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {if (rootView == null) {rootView = inflater.inflate(R.layout.fragment_home, null);T.showShort(getActivity(), "FragmentHome==onCreateView");}ViewGroup parent = (ViewGroup) rootView.getParent();if (parent != null) {parent.removeView(rootView);}return rootView;}}

版权声明:本文为博主原创文章,未经博主允许不得转载。

当爱丽思丢失了通往仙境的钥匙,

重写FragmentTabHost,防止FragmentTabHost切换fragment重新调用

相关文章:

你感兴趣的文章:

标签云: