FragmentTabHost onChoose Tab onActivityCreate Again
FragmentTabHost onChoose Tab onActivityCreate Again
在 FragmentTabHost 中选择选项卡 onActivityCreated 时再次创建并刷新所有内容并创建新的
如何解决这个问题?不要再创建了?
ViewPager 和 FragmentTabHost 中的片段生命周期有何不同?
为什么要重新创建 FragmentShop?
FragmentShop
public class FragmentShop extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_shop, null);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lstShop = (ListView) getView().findViewById(R.id.lstShop);
lytPrgBar = (LinearLayout) getView().findViewById(R.id.lytShopprogressBar);
btnFloatAndswipeRefresh();
sendAndGetDataWithListener();
showDotedAnimate();
AnimateBtnFloatOnListScroll();
}
FragmentTabHost
片段市场
public class FragmentMarket extends Fragment {
FragmentTabHost mTabHost;
public static Fragment instance() {
Fragment fragment = new FragmentMarket();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_market, container, false);
mTabHost = (FragmentTabHost) rootView.findViewById(R.id.market_tabhost);
mTabHost.setBackgroundResource(R.color.BlueLight);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.market_realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("sales").setIndicator("خرید و فروش"), FragmentSales.class, null);
mTabHost.addTab(mTabHost.newTabSpec("estate").setIndicator("مشاوران املاک"), FragmentEstate.class, null);
mTabHost.addTab(mTabHost.newTabSpec("shop").setIndicator("معرفی بازار"), FragmentShop.class, null);
mTabHost.getTabWidget().getChildAt(0).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
mTabHost.getTabWidget().getChildAt(1).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
mTabHost.getTabWidget().getChildAt(2).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
关于片段实例():
此方法只是为 ViewPager 创建页面 这是适配器:
public class AdapterViewPager extends FragmentPagerAdapter {
public AdapterViewPager(Context context, FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "مارکت";
case 1:
return "مجله امروز";
case 2:
return "رویدادها";
case 3:
return "آخرین اخبار";
case 4:
return "دانش آموزان برتر";
case 5:
return "قرعه کشی و جایزه";
case 6:
return "زیبایی و زشتی های شهر";
case 7:
return "حمایت";
default:
return "آخرین اخبار";
}
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FragmentMarket.instance();
case 1:
return FragmentMag.instance();
case 2:
return FragmentEvents.instance();
case 3:
return FragmentNews.instance();
case 4:
return FragmentStudent.instance();
case 5:
return FragmentReward.instance();
case 6:
return FragmentGoodBad.instance();
case 7:
return FragmentSupport.instance();
default:
return FragmentNews.instance();
}
}
@Override
public int getCount() {
return 8;
}
}
试试这个。
public FragmentMarket getTestFragment() {
if (marketFragment == null) {
marketFragment = new FragmentMarket ();
}
return marketFragment;
}
最终解决方案:
这是片段生命周期:
https://developer.android.com/guide/components/fragments.html#Creating
在 onDestroyView 之后销毁所有视图但不销毁变量
我只是在 FragmentShop 中这样做
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lstSales = (ListView) getView().findViewById(R.id.lstSales);
lytPrgBar = (LinearLayout) getView().findViewById(R.id.lytSalesprogressBar);
btnFloatAndswipeRefresh();
showDotedAnimate();
AnimateBtnFloatOnListScroll();
if (salesTitle.isEmpty()) {
sendAndGetDataWithListener();
} else {
arrayStructList.clear();
salesDatalistener.onDataRecivce();
}
}
所以你只需要重新创建所有视图并设置参数并且不获取新数据而我只有 ListView 和浮动按钮
这可以帮助但不适合我:https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en
在 FragmentTabHost 中选择选项卡 onActivityCreated 时再次创建并刷新所有内容并创建新的
如何解决这个问题?不要再创建了?
ViewPager 和 FragmentTabHost 中的片段生命周期有何不同?
为什么要重新创建 FragmentShop?
FragmentShop
public class FragmentShop extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_shop, null);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lstShop = (ListView) getView().findViewById(R.id.lstShop);
lytPrgBar = (LinearLayout) getView().findViewById(R.id.lytShopprogressBar);
btnFloatAndswipeRefresh();
sendAndGetDataWithListener();
showDotedAnimate();
AnimateBtnFloatOnListScroll();
}
FragmentTabHost
片段市场
public class FragmentMarket extends Fragment {
FragmentTabHost mTabHost;
public static Fragment instance() {
Fragment fragment = new FragmentMarket();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_market, container, false);
mTabHost = (FragmentTabHost) rootView.findViewById(R.id.market_tabhost);
mTabHost.setBackgroundResource(R.color.BlueLight);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.market_realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("sales").setIndicator("خرید و فروش"), FragmentSales.class, null);
mTabHost.addTab(mTabHost.newTabSpec("estate").setIndicator("مشاوران املاک"), FragmentEstate.class, null);
mTabHost.addTab(mTabHost.newTabSpec("shop").setIndicator("معرفی بازار"), FragmentShop.class, null);
mTabHost.getTabWidget().getChildAt(0).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
mTabHost.getTabWidget().getChildAt(1).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
mTabHost.getTabWidget().getChildAt(2).getBackground().setColorFilter(R.color.BlueDark, Mode.MULTIPLY);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
关于片段实例(): 此方法只是为 ViewPager 创建页面 这是适配器:
public class AdapterViewPager extends FragmentPagerAdapter {
public AdapterViewPager(Context context, FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "مارکت";
case 1:
return "مجله امروز";
case 2:
return "رویدادها";
case 3:
return "آخرین اخبار";
case 4:
return "دانش آموزان برتر";
case 5:
return "قرعه کشی و جایزه";
case 6:
return "زیبایی و زشتی های شهر";
case 7:
return "حمایت";
default:
return "آخرین اخبار";
}
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FragmentMarket.instance();
case 1:
return FragmentMag.instance();
case 2:
return FragmentEvents.instance();
case 3:
return FragmentNews.instance();
case 4:
return FragmentStudent.instance();
case 5:
return FragmentReward.instance();
case 6:
return FragmentGoodBad.instance();
case 7:
return FragmentSupport.instance();
default:
return FragmentNews.instance();
}
}
@Override
public int getCount() {
return 8;
}
}
试试这个。
public FragmentMarket getTestFragment() {
if (marketFragment == null) {
marketFragment = new FragmentMarket ();
}
return marketFragment;
}
最终解决方案:
这是片段生命周期: https://developer.android.com/guide/components/fragments.html#Creating
在 onDestroyView 之后销毁所有视图但不销毁变量
我只是在 FragmentShop 中这样做
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lstSales = (ListView) getView().findViewById(R.id.lstSales);
lytPrgBar = (LinearLayout) getView().findViewById(R.id.lytSalesprogressBar);
btnFloatAndswipeRefresh();
showDotedAnimate();
AnimateBtnFloatOnListScroll();
if (salesTitle.isEmpty()) {
sendAndGetDataWithListener();
} else {
arrayStructList.clear();
salesDatalistener.onDataRecivce();
}
}
所以你只需要重新创建所有视图并设置参数并且不获取新数据而我只有 ListView 和浮动按钮
这可以帮助但不适合我:https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en