1

I have a tabbed activity 1 to 5. If I move from 3 to 5 then on back pressed I want to switch back from 5 to 3 i.e. on back pressed I want to move back to the previous tab. Please help

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

Use the below code to achieve it.(Code is tested also.)

1.Initialise a List of integers and a boolean value;

private boolean isBackPressed = false;  
private boolean isBackPressedOnce = false;   
private List<Integer> tabsInBack = new ArrayList<>();

2.Now your onTabSelectedListener has to be like

tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
      @Override
      public void onTabSelected(TabLayout.Tab tab) {
        if(!isBackPressed){
          tabsInBack.add(tab.getPosition());}
        else {
              isBackPressed = false;}              
      }

      @Override
      public void onTabUnselected(TabLayout.Tab tab) {

      }

      @Override
      public void onTabReselected(TabLayout.Tab tab) {

      }
    });

3(i).Now your onBackPressed() function has to be,(One tab back then close)

    @Override
      public void onBackPressed() {
        isBackPressed = true;
        if (tabsInBack != null && tabsInBack.size() > 0) {
          if (tabs.getSelectedTabPosition() == tabsInBack.get(tabsInBack.size() - 1)) {
            tabsInBack.remove(tabsInBack.size() - 1);

          }
          if (tabsInBack != null && tabsInBack.size() > 0) {
            tabs.getTabAt(tabsInBack.get(tabsInBack.size() - 1)).select();
            tabsInBack.remove(tabsInBack.size() - 1);

          } else {
            super.onBackPressed();
          }
        } else {
          super.onBackPressed();
        }
      }

3(ii).Now your onBackPressed() function has to be,(Back to all selected tabs and then close)

    @Override
      public void onBackPressed() {
        isBackPressed = true;
        if (tabsInBack != null && tabsInBack.size() > 0) {
          if (tabs.getSelectedTabPosition() == tabsInBack.get(tabsInBack.size() - 1)) {
            tabsInBack.remove(tabsInBack.size() - 1);

          }
          if (tabsInBack != null && tabsInBack.size() > 0) {
            tabs.getTabAt(tabsInBack.get(tabsInBack.size() - 1)).select();
            tabsInBack.remove(tabsInBack.size() - 1);
             if(isBackPressedOnce )
               {
                 super.onBackPressed();
               }
            else
               {
                 isBackPressedOnce = true;
               }
          } else {
            super.onBackPressed();
          }
        } else {
          super.onBackPressed();
        }
      }

Let me know whether it helped or not.

Sachin Varma
  • 2,175
  • 4
  • 28
  • 39
  • I dont want list of previous tabs...just want only one tab back...if i am at tab 5 then if at 3 then on back pressed it must come to 5 again...thats it... – meenakshi agrawal Dec 07 '17 at 13:58
  • Answer is updated,please have a look, while replacing please take all the code from updated answers.Done some logical changes. – Sachin Varma Dec 07 '17 at 15:23
  • thanks for your code it is very helpful and almost resolves my problem.I tested the code and finds the there is no use of variable "isBackPressedOnce ".It is creating problem even. Now I am not getting how to add tab 1 in list as it is selected by default so tablistener is not listening it.....so ifi move from 1 to 3 then 3 to 5 then on backpressed i can move from 5 to 3 but on backpressed again i moved to previous activity not to tab 1.... – meenakshi agrawal Dec 09 '17 at 12:03
  • should I add tab 1 statically at the beginning of the list? – meenakshi agrawal Dec 09 '17 at 12:07
  • As u have mentioned in first comment, that u only need one tab back,i have changed the code like that. Tab one will be automatically selected .I updated code for both one tab back and whole tabs back.If it was helpful to you please mark the answer as accepted and vote up. – Sachin Varma Dec 09 '17 at 13:17
0

This works for me :) It works exactly like whatsapp

@Override public void onBackPressed() {

    //If first tab is open, then quit
    if (viewPager.getCurrentItem() == 0) {

        super.onBackPressed();
    }else {

        //If any other tab is open, then switch to first tab
        viewPager.setCurrentItem(0);
    }

}
ASAD HAMEED
  • 2,296
  • 1
  • 20
  • 36