When one fragment A is changed to another fragment B, it will call onCreate()
and some other functions to do initialization. After the Fragment
has changed, the fragments near the current fragment also will be initialized. For example: I have 5 fragments A, B, C, D, E. The current fragment is A. When I change to D, D along with C and E will be initialized. My Problem is:
Some codes are very slow to execute so I want to initialize a part of the data instead of ALL of the data. That is, when the fragment is visible, then some data will be initialized. When its not the current fragment, the initialization will be delayed. Here is a pic:
I use these classes:
android.widget.TabHost
android.support.v4.app.Fragment
android.support.v4.app.FragmentActivity
android.support.v4.app.FragmentPagerAdapter
android.support.v4.view.ViewPager
I read the API but there is no method called when the fragment is VISIBLE. So, I think I can do something with onTabChanged
. But I don't know how to get the content fragment through TabHost
. If I can, then I will call my own function to do initialization.
- How can I know the fragment is hide or its the current one? OR
- Can I get the content fragment in TabHost? OR
- Other ways to delay the initialization.
Here is some code:
public class TabsAdapter extends FragmentPagerAdapter
implements TabHost.OnTabChangeListener,
ViewPager.OnPageChangeListener{
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private static final class TabInfo {
private final Class<?> mClss;
private final Bundle mArgs;
TabInfo(String _tag, Class<?> _class, Bundle _args) {
mClss = _class;
mArgs = _args;
}
}
public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
/** MainActivity call this func to add tabs */
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
some code
}
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
Log.i("demo tab","onPageSelected tab change to--------- " + position);
}
}
MainActivity: /** these code is to create fragment */
FrameLayout tabContact = (FrameLayout) LayoutInflater.from(this).inflate(R.layout.tab_widget, null);
ImageView imgBackContact = (ImageView) tabContact.findViewById(R.id.tab_widget_back_image);
ImageView imgFrontContact = (ImageView) tabContact.findViewById(R.id.tab_widget_front_image);
imgBackContact.setImageResource(R.drawable.ic_tab_contact_normal);
imgFrontContact.setImageResource(R.drawable.ic_tab_contact_selected);
/** these code is to put fragments into TabHost */
mTabsAdapter.addTab(mTabHost.newTabSpec(XXX), A.class, null);