title: How to move to a specific tab from an activity in android
body: <p>I am working on the following screen named FriendListActivity:</p>
  
  <p><a href="https://i.stack.imgur.com/shMIz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/shMIz.png" alt="Screenshot"></a>
  As you can see here , I am 3 tabs named CHAT,GROUPS and CONTACTS .Each tab is displaying some contents.For example Groups Tab is displaying the number of groups.On clicking each Group ,Group Chat Screen is opened .Now In Group Chat screen ,i have a toolbar .Consider the following code:</p>
  
  <pre><code> Toolbar toolbarGroupChat = (Toolbar) findViewById(R.id.toolbarGroupChat);
    setSupportActionBar(toolbarGroupChat);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      getSupportActionBar().setTitle(upperCase(group_name));
  
  
    //ViewPager
      viewPager = (ViewPager) findViewById(R.id.viewPager);
      //Initializing PagerAdapter
      PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
      viewPager.setAdapter(pagerAdapter);
  
      viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
          @Override
          public void onTabSelected(TabLayout.Tab tab) {
              viewPager.setCurrentItem(tab.getPosition());
          }
  
          @Override
          public void onTabUnselected(TabLayout.Tab tab) {
  
          }
  
          @Override
          public void onTabReselected(TabLayout.Tab tab) {
  
          }
      });
  </code></pre>
  
  <p><strong>PagerAdapter.java</strong></p>
  
  <pre><code>    public class PagerAdapter extends FragmentStatePagerAdapter {
      private int noOfTabs;
  
      public PagerAdapter(FragmentManager fm, int noOfTabs) {
          super(fm);
          this.noOfTabs = noOfTabs;
      }
  
      @Override
      public Fragment getItem(int position) {
          switch (position) {
              case 0:
                  ChatFragment chatFragment = new ChatFragment();
                  return chatFragment;
              case 1:
                  GroupsFragment groupsFragment = new GroupsFragment();
                  return groupsFragment;
              case 2:
                  ContactsFragment contactsFragment = new ContactsFragment();
                  return contactsFragment;
              default:
                  return null;
          }
      }
  
      @Override
      public int getCount() {
          return noOfTabs;
      }
  }
  </code></pre>
  
  <p>On clicking the arrow on the toolbar ,my application is moved to the screen containing three tabs.My problem is current selected tab is always CHAT tab ,when i am moving from an activity to FriendListActivity .When i am moving from Group Chat Screen to FriendListActivity,the current selected tab should be GROUPS Tab.</p>
  
  <hr>
  
  <p>Solved.</p>
  
  <p>Edited Working Code: 
  When we are moving from an Activity to a specific tab ,We need to pass the TAb number using intents from the Activity.Use the following code for handling click on the back button of the Toolbar .Override the getSupportParentActivityIntent() method:</p>
  
  <pre><code>public Intent getSupportParentActivityIntent() {
      final Bundle bundle = new Bundle();
      final Intent intent = new Intent(this, FriendsListActivity.class);
      bundle.putString("TabNumber", tab_number);
      intent.putExtras(bundle);
      return intent;
  }
  </code></pre>
  
  <p>Now inside the activity containing 3 tabs,use the following code:</p>
  
  <pre><code>     final Intent intent = getIntent();
          if (intent.hasExtra("TabNumber")) {
              String tab = intent.getExtras().getString("TabNumber");
              Log.e("TabNumberFriendList",tab);
              switchToTab(tab);
          }
   public void switchToTab(String tab){
           if(tab.equals("0")){
               viewPager.setCurrentItem(0);
           }else if(tab.equals("1")){
               viewPager.setCurrentItem(1);
           }else if(tab.equals("2")){
               viewPager.setCurrentItem(2);
           }
       }
  </code></pre>