Search code examples
javaandroidandroid-fragmentsandroid-tablayoutandroid-tabs

What is the lifecycle of tabs, when switching between Tabs in TabLayout


I have five tabs in Tablayout with fragments. I have a Floating button at the Bottom right corner of the first Tab. when I click the FAB button, it inflates a small CardView(Initially Cardview was set to INVISIBLE), and when I click FAB button again, Cardview goes to invisible state. (To achieve that, I have used 'flags' true and false).

Now, my requirement is--- Suppose, I am in first Tab. And I clicked the FAB button, in return, it inflated Cardview. Without clicking the FAB button again(Cardview is in VISIBLE state) I went to second Tab. Then, I moved to First Tab from the second Tab. but, still the Cardview stays VISIBLE state.

I want to make the Cardview INVISIBLE when I move to other Tabs and come back.

please tell me how to achieve that functionality.

Thanks in advance.


Solution

  • What is the lifecycle of tabs, when switching between Tabs in TabLayout

    Kindly go through this Answer of mine to understand what happens when you switch the tabs.

    I want to make the Cardview INVISIBLE when I move to other Tabs and come back.

    You can utilize setOnTabSelectedListener of TabLayout

    tabLayout.setOnTabSelectedListener(
      new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            super.onTabSelected(tab);
            // Do your login here.. according to tab positon
        }
    });