androidandroid-intenttabactivityandroid-tabactivity

Intent to open a specific tab of tabbed activity


I have a tabbed activity with 5 tabs. Each tab has only one Imageview. On a previous page I have 5 buttons and I want to create an interface such that each button starts the tabbed activity but the first tab which is visible is specific to that button. eg. gallery apps open a specific tab corresponding to the thumnail of the photo and are also left/right swappable.


Solution

  • You can pass the tab id you want to open as an extra to the Intent you are creating. Then in the tabbed Activity, assuming you are using TabLayout, you can do something like this -

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    TabLayout.Tab tab = tabLayout.getTabAt(getIntent().getStringExtra("selected_index"));
    tab.select();