Search code examples
androidandroid-tablayoutandroid-tabstabactivity

Start a specific tab of a tab layout (with switch cases) from different activity


I have this onClickListener on a card view of dashboard activity which will open a specific tab of a tab layout. Calling it from dashboard activity to category activity. This is what I did inside onclicklistener.

        grocery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(DashboardActivity.this, CategoryActivity.class);
            intent.putExtra("position", 2);
            startActivity(intent);

        }
    });

This is my category activity

public class CategoryActivity extends AppCompatActivity {

public SectionsPagerAdapter mSectionsPagerAdapter;
public ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category);

    Intent intent = getIntent();
    if(intent == null) {
        extrasPosition = 0;
    } else {
        extrasPosition = intent.getIntExtra("position",0);
    }
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
//display the desired fragment
    FragmentTransaction frt = getSupportFragmentManager().beginTransaction();
    frt.replace(R.id.container, mSectionsPagerAdapter.getItem(extrasPosition));
    frt.addToBackStack(null);
    frt.commit();

}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // returning current tabs using switch case
        switch (position){
            case 0:
                viewClothing tab0 = new viewClothing();
                return tab0;
            case 1:
                viewElectronics tab1 = new viewElectronics();
                return tab1;
            case 2:
                viewFurniture tab2 = new viewFurniture();
                return tab2;
            case 3:
                viewGrocery tab3 = new viewGrocery();
                return tab3;
            case 4:
                viewHardware tab4 = new viewHardware();
                return tab4;
            case 5:
                viewStationary tab5 = new viewStationary();
                return tab5;
            case 6:
                viewOthers tab6 = new viewOthers();
                return tab6;
                default:
                    return null;
        }
    }
    @Override
    public int getCount() {
        // Show 7 total pages.
        return 7;
    }
}
}

What I want is to open the grocery tab which is case 3. Thanks in advance.


Solution

  • First get int extra from intent you're sending. Then if it's not null you invoke getItem(position) with default 0 and if it's not you pass the position and display the desired fragment.

     public class CategoryActivity extends AppCompatActivity {
    
    public SectionsPagerAdapter mSectionsPagerAdapter;
    public ViewPager mViewPager;
    public int extrasPosition;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        //get extra from intent;
        Intent intent = getIntent();
        if(intent == null) {
            extrasPosition = 0;
        } else {
            extrasPosition = intent.getIntExtra("position", 0);
        }
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    
        //display the desired fragment
        mViewPager.setCurrentItem(extrasPosition);
    }
    
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            // returning current tabs using switch case
            switch (position){
                case 0:
                    viewClothing tab0 = new viewClothing();
                    return tab0;
                case 1:
                    viewElectronics tab1 = new viewElectronics();
                    return tab1;
                case 2:
                    viewFurniture tab2 = new viewFurniture();
                    return tab2;
                case 3:
                    viewGrocery tab3 = new viewGrocery();
                    return tab3;
                case 4:
                    viewHardware tab4 = new viewHardware();
                    return tab4;
                case 5:
                    viewStationary tab5 = new viewStationary();
                    return tab5;
                case 6:
                    viewOthers tab6 = new viewOthers();
                    return tab6;
                    default:
                        return null;
            }
        }
        @Override
        public int getCount() {
            // Show 7 total pages.
            return 7;
        }
    }
    }