Search code examples
androidandroid-toolbarandroid-tablayout

How to move to a specific tab from an activity in android


I am working on the following screen named FriendListActivity:

Screenshot 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:

 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) {

        }
    });

PagerAdapter.java

    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;
    }
}

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.


Solved.

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:

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;
}

Now inside the activity containing 3 tabs,use the following 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);
         }
     }

Solution

  • Use the setCurrentItem(int position) method of your ViewPager object to move to a specific tab.