Android : onCreateView fragment on tabhost called every time I change the tab

on Saturday, September 13, 2014


I am using tab host with fragment, the following is the code of the main activity



public class HomeActivity extends Activity{

private FragmentTabHost mTabHost;
private ArrayList<CustomTabIndicator> mCustomTabIndicator;
private ArrayList<BaseFragment> mTabFragments;


private class CustomTabIndicator {

private int mIdResId;
private int mTitleResId;
private int mIconResId;

public CustomTabIndicator(int idResId, int titleResId, int iconResId) {

this.mIdResId = idResId;
this.mTitleResId = titleResId;
this.mIconResId = iconResId;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

initializeTabIndicatorsAndFragments();
addTabIndicatorsToTabHost();
}

private void initializeTabIndicatorsAndFragments() {

mCustomTabIndicator = new ArrayList<HomeActivity.CustomTabIndicator>();

mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_dashboard,
R.string.tab_dashboard, R.drawable.tab_dashboard));

mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_feed,
R.string.tab_feed, R.drawable.tab_feed));

mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_lists,
R.string.tab_lists, R.drawable.tab_lists));

mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_me,
R.string.tab_me, R.drawable.tab_me));

mTabFragments = new ArrayList<BaseFragment>();


mTabFragments.add(new DashboardFragment());
mTabFragments.add(new FeedFragment());
mTabFragments.add(new ListsFragment());
mTabFragments.add(new MeFragment());
}

private void addTabIndicatorsToTabHost() {

for (int i = 0; i < mCustomTabIndicator.size(); i++) {
mTabHost.addTab(
mTabHost.newTabSpec(
getString(mCustomTabIndicator.get(i).mIdResId))
.setIndicator(
createTabView(
this,
mCustomTabIndicator.get(i).mTitleResId,
mCustomTabIndicator.get(i).mIconResId)),
mTabFragments.get(i).getClass(), null);
}

}

@SuppressLint("InflateParams")
private View createTabView(final Context context, final int textStringId,
final int imageResId) {

View view = LayoutInflater.from(context).inflate(
R.layout.fragment_tab_header_image_text_layout, null);

ImageView tabIV = (ImageView) view.findViewById(R.id.tab_icon);
tabIV.setImageResource(imageResId);
//TextView titleTV = (TextView) view.findViewById(R.id.tab_title);
//titleTV.setText(textStringId);
return view;
}
}


This is the code of one of the fragements



public class MeFragment extends BaseFragment {

private View mFragementView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

Log.i("AMIRA", "MeFragment - onCreateView");
mFragementView = inflater.inflate(R.layout.fragment_me_screen,container, false);

initializeUIComponents();
initializeUIComponentsData();
initializeUIComponentsTheme();
initializeUIComponentsAction();

return mFragementView;

}
}


The problem now that onCreateView called every time I change the tab, and take long time to render and draw the content of fragment.


So I have tried the following code



public class MeFragment extends BaseFragment {

private View mFragementView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

if (mFragementView != null) {
return mFragementView;
} else {
Log.i("AMIRA", "MeFragment - onCreateView");
mFragementView = inflater.inflate(R.layout.fragment_me_screen,container, false);

initializeUIComponents();
initializeUIComponentsData();
initializeUIComponentsTheme();
initializeUIComponentsAction();

return mFragementView;
}

}
}


and i got the following exception



java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


so can anyone help here ?


0 comments:

Post a Comment