youssef abaza
youssef abaza

Reputation: 145

How to choose which Fragment opens first?

I am working with an ActionBar activity hosting three fragments (Fragments A,B,C) with tabs. The thing is when you open the activity is shows fragment A since it's the first one so I am trying to make a button that opens fragment C directly and an other button that opens fragment B directly. any ideas on how to make that, thanks :D

Upvotes: 0

Views: 185

Answers (1)

DavidGSola
DavidGSola

Reputation: 727

Activity with tabs:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    int position = extras.getInt("position");

    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mViewPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
    mViewPager.setAdapter(mViewPagerAdapter);

    // etc etc etc

    mViewPager.setCurrentItem(position);
}

And pass the position desired using a Bundle on your first Activity:

 Intent intent = new Intent(this, SecondActivity.class);
 i.putExtra("position", positionTabDesired);
 startActivity(intent);

Upvotes: 1

Related Questions