Search code examples
androidandroid-viewpagerandroid-tablayout

Calling tab of a Activity from other Activity


I have a MainActivity and I am using android.support.design.widget.TabLayout for the tabview with Viewpager.

Now If i am in some other activity and from this activity if I want to go to the 3rd tab of the MainActivity, how to achieve this.

Thanks in advance.


Solution

  • Do the following.

    at the MainActivity class at the onCreate method after initializing the tabLayout write below code

    int index = getIntent().getIntExtra("SelectedIndex",0);
    tabLayoutObject.getTabAt(index).select();
    

    And from place from where you want to redirect to MainActivity write following

    Intent intent = new Intent(this,MainActivity.class);
    intent.putExtra("SelectedIndex",2) //what ever the index is
    startActivity(intent);
    

    Hope this works for you