0

I have used viewpager with tablayout in activity.It have 3 tab, The issue which faced is when selected tabs 1&2 back button is not working,and its not able to go to the previous activity. And, in 3rd tab back button is working and I am able to access the previous activity.

I have given finish code in onBack function. Then also Its not going to the previous activity.

Tell me how to set back button for Tab 1 & Tab 2 and go to the previous activity. Please help me as soon as possible. Thanks in advance

enter code here

class MainActivity {

TabLayout.Tab firsttab = tabs.newTab();
firsttab.setText("Personal Details");
tabs.addTab(firsttab);

TabLayout.Tab secondtab = tabs.newTab();
secondtab.setText("Employment");
tabs.addTab(secondtab);

TabLayout.Tab thirdtab = tabs.newTab();
thirdtab.setText("Documents");
tabs.addTab(thirdtab);


Servicepageadapter adapter = new Servicepageadapter(getSupportFragmentManager(),tabs.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        tapposition = tab.getPosition();

        viewPager.setCurrentItem(tab.getPosition());
    }

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

    }

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

    }
});

private class Servicepageadapter extends FragmentPagerAdapter {
    int mNumOfTabs ;
    public Servicepageadapter(FragmentManager fragmentManager, int tabCount) {
        super(fragmentManager);
        this.mNumOfTabs = tabCount;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position){
            case 0:
                boolean  isbackstack = true;
                PersonalFragment tab1 = new PersonalFragment().newInstance("", "","");
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

                if (!isbackstack) {
                    transaction.addToBackStack(null);
                }
                transaction.commit();
               return tab1;

            case 1:

                boolean  isbackstack1 = true;
                EmploymentFragment tab2 = new EmploymentFragment().newInstance("", "","");
                FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();

                if (!isbackstack1) {
                    transaction1.addToBackStack(null);
                }
                transaction1.commit();
               return tab2;

            case 2:
                boolean isbackstack2 = true;
                DocumentsFragment tab3 = new DocumentsFragment().newInstance("", "","");
                FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
                if (!isbackstack2) {
                    transaction2.addToBackStack(null);
                }
                transaction2.commit();
                return tab3;
            default:
                return null;
        }

    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.