Reputation: 992
I used a ViewPager and an ActionBar with NavigationMode : "NAVIGATION_MODE_TABS"
I put 3 Fragments : Fragment0 (left tab) ,Fragment1 (middle tab),Fragment2(right tab)
First, the Fragment0 appears and the methods "onCreateView" and "onStart" are called for Fragment0 AND the Fragment1
If I slide horizontally to go to the "middle tab"(Fragment1), the methods "onCreateView" and "onStart" of Fragment2(right tab) are called...
So, I want to know if this is normal ?
Because I want to call "onStart" of Fragment2 just before it appears, How do it?
My code :
public class ActivityPrincipale extends FragmentActivity {
private Intent intentService1;
private Fragment fragment;
[..]
private ActionBar.TabListener tabListener;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.viewpager);
// Création de la liste de Fragments que fera défiler le PagerAdapter
listFragments = new Vector();
// Ajout des Fragments dans la liste des fragments
listFragments.add(Fragment.instantiate(this, Fragment0.class.getName()));
listFragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
listFragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
// Création de l'adapter qui s'occupera de l'affichage de la liste de Fragments
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), listFragments);
pagerView = (ViewPager) super.findViewById(R.id.viewpager);
// affectation de l'adapter au ViewPager
pagerView.setAdapter(this.mPagerAdapter);
pagerView.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// Quand il y a un swapp entre les page
// on selectionne le tab correspondant
Log.w("SWAPPING FRAGMENT!!", "swapping de fragment avec comme positin de destination = " + position);
getActionBar().setSelectedNavigationItem(position); // DEBUGMODE
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
pagerView.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
actionBar.addTab(actionBar.newTab().setText("Frag0").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Frag1"").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Frag2").setTabListener(tabListener));
}
}
One Fragment :
public class Fragment0 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_0, container, false);
Log.w("oFragmento", "fragment0");
return rootView;
}
}
My PagerAdapter :
public class MyPagerAdapter extends FragmentPagerAdapter {
private final List fragments;
public MyPagerAdapter(FragmentManager fm, List list_fragments) {
super(fm);
this.fragments = list_fragments;
}
@Override
public Fragment getItem(int position) {
return (Fragment) fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
Upvotes: 6
Views: 15859
Reputation: 8747
That is completely normal for a Fragment to go through onCreateView
then onStart
. You may want to take a look at the Fragment Lifecycle.
If I understand you correctly when you say "just before it appears", then I think the lifecycle method you are looking for is the onResume
method. This is the last one to be called before the fragment is active.
Upvotes: 4