3

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.

fadden
  • 51,356
  • 5
  • 116
  • 166
Ravi Kumar Seth
  • 154
  • 2
  • 14

2 Answers2

3

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();
jaibatrik
  • 6,770
  • 9
  • 33
  • 62
  • What if we want to open 2nd Tab when activity gets started without login and we have a check at first tab of login which redirects at login screen. – Anshul Tyagi Jul 24 '17 at 12:35
2

Try This

  1. First activity

     int page = 2;
     Intent intent = new Intent(FirstActivity.this,TabActivityClass.class);
     intent.putExtra("One", page);// One is your argument 
     startActivity(intent);
    

    2.In oncreate method of TabActivity class

    int defaultValue = 0;
    int page = getIntent().getIntExtra("One", defaultValue);
    viewPager.setCurrentItem(page);
    
Sunil
  • 3,785
  • 1
  • 32
  • 43