2

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);
         }
     }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Deepak Rattan
  • 1,279
  • 7
  • 21
  • 47

4 Answers4

9

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

Varun Kumar
  • 1,241
  • 1
  • 9
  • 18
  • Thanks for the reply.I am already using the setCurrentItem() method.You can check my edited code.In my case each tab is displaying some contents of list view. e.g. Groups tab is displaying the list of groups .In clicking a specific group ,Group Chat Is opened .Now in Group Chat Screen ,On clicking the arrow on the toolbar i am moving back to the activity containing Three tabs.As i am returning form Group Chat Screen,Tab selected should be GROUPS TAB.But it is always showing CHAT tab as selected TAB. – Deepak Rattan Apr 13 '16 at 06:16
  • Sorry for not being specific enough. I'm not talking about using setCurrentItem() method inside OnTabSelectedListener(). – Varun Kumar Apr 13 '16 at 06:22
  • When you return back from Group Chat to your Activity, you need to set the current item there by help of setCurrentItem() method otherwise, it will go to the default position which is zero i.e. your first tab. – Varun Kumar Apr 13 '16 at 06:25
  • You will need to detect from which Activity you have returned back from, chat or group or contacts and then set the current item inside the ViewPager. – Varun Kumar Apr 13 '16 at 06:26
  • Thanks for the reply,But still i am not getting the point .On clicking the back button of the Toolbar i am supposed to move to the previous TAB.For this i am using the code Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); – Deepak Rattan Apr 13 '16 at 06:33
  • This is added in the manifest file – Deepak Rattan Apr 13 '16 at 06:34
  • Clicking on back button takes you to previous Activity, not tab unless you have overridden the default behaviour. – Varun Kumar Apr 13 '16 at 06:37
  • Manifest file code which you mentioned above is only responsible for changing your Activity i.e bringing you back to your parent Activity from your Child Activity. It is not responsible for changing your tab position. – Varun Kumar Apr 13 '16 at 06:38
  • When you return back to your Parent Activity from your Child Activity, you need to specify the position for your tab inside the setCurrentItem() method of view pager. Unless you do this it will return back to default or zero position i.e. first tab. – Varun Kumar Apr 13 '16 at 06:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109006/discussion-between-deepakr-and-varun-kumar). – Deepak Rattan Apr 13 '16 at 06:41
  • Sure, I'm there in chat. – Varun Kumar Apr 13 '16 at 06:43
  • As you can see i have three tabs.On clicking Group Tab ,A list view is opened which contains list of groups .On clicking a specific group ,Group Chat Activity is opened .On clicking the back button on the toolbar ,Group Tab is the current tab.It is working fine for this.But for CHAT tab and CONTACTS TAB ,it is not working .It always give me the CHAT TAB.On clicking the items of CHAT TAB And CONTACTS Tab, SingleChatActivity is opened. I am passing tab number through intent to SIngleChatACtivity.But it is not working for me. – Deepak Rattan Apr 13 '16 at 08:23
  • Hi Deepak. I've seen your updated code and have replied you in the chat session. Please do have a look. – Varun Kumar Apr 13 '16 at 13:14
1

You might have created a tabHost, so when it is done making, you should use -

tabHost.setCurrentTab(1);

and you can use same code with different parameters whenever you want to set another tab.

Ref

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
0

From the screenshot you must be using Tablayout, so from there you can use these two in build functions.

tabLayout.getSelectedTabPosition();

Save this tab in some variable and then onCreate set that integer value in this function..

tabLayout.getTabAt(<Your last selected tab>);

Hope this helps..:)

0

You can also use this piece of code:

Add public static TabToOpen = 1 in the group chat activity(say group_chat_activity).

then use setCurrentTab(group_chat_activity.TabToOpen) in onResume() of activity where your ViewPager object is to move to a specific tab. This is particularly useful when you need a button to access a particular tab in sliding activity.