1

I have a TabLayout with three fragments, on the first tab i want to switch between two fragment depending on a condition, i used if statement but still the fragments are not switching it only retains its initial fragment. The following is the case scenario and a code snippet of what am doing.

MY app allows user to decide if they are to login at first use or not, and my main activity is the launcher, the main activity's(Containing a TabLayout and three Fragment) adapter checks if the user is logged in or not.            

if (user != login) {
  load fragment A
}else{
  load fragment B
}

I have an option somewhere where user can log in while using the app, if user clicks on login button, it loads a fragement for user to login, once the user is logged in, the fragment still retain the state of user not logged in i.e its still loading fragment A, but then i can see all user login details in the app.                                                

this point is still active      

                                   

if (user != login) {
   load fragment A
} 

Code Snippet

@Override
public Fragment getItem(int position) {
    Fragment f;
    switch (position) {
        case NEWS_POST:
            if(fbid != null){
                f = new NewsFragment();
            }else{
                f = new NewsAllFragment();
            }
Jean Vitor
  • 893
  • 1
  • 18
  • 24
SimpuMind
  • 365
  • 8
  • 22
  • please provide more detail, your issue is incomplete – Zaid Mirza Aug 15 '16 at 11:51
  • Add a bit more code, the part where you are using `FragmentManager` to `add/replace` the current `Fragment`. Also add breakpoints where you are initializing `NewsFragment` and `NewsAllFragment` to check if it is actually being called. – Abbas Aug 15 '16 at 11:54
  • You should be storing a loggedIn boolean or some key in your preference to check if you want to know if the user has logged in or not – Eenvincible Aug 15 '16 at 12:00

1 Answers1

0

First you should use SharedPreferences to check user is logged in or not.

Moreover you can use this code in your fragments to check if this fragment is visible or not. and if visible you can do your required task getting boolean logged in information from shared preferences.

private static boolean m_iAmVisible;

@Override
public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser);
    m_iAmVisible = isVisibleToUser;

    if (m_iAmVisible) { 
        Log.d(localTAG, "this fragment is now visible");
    } else {  
        Log.d(localTAG, "this fragment is now invisible");
    }
}

Here see i have a fragment in which i am using viewPager, in onCreateView Method

final View x = inflater.inflate(R.layout.consumer_tab_layout, null);
        tabLayout = (TabLayout) x.findViewById(R.id.tabs);
        viewpager_for_tabs = (ViewPager) x.findViewById(R.id.viewpager_for_tabs);


        bottom_tv1 = (TextView) x.findViewById(R.id.bottom_tv1);
        bottom_tv2 = (TextView) x.findViewById(R.id.bottom_tv2);
        bottom_tv3 = (TextView) x.findViewById(R.id.bottom_tv3);
        bottom_tv4 = (TextView) x.findViewById(R.id.bottom_tv4);
        bottom_tv5 = (TextView) x.findViewById(R.id.bottom_tv5);

        /**
         *Set an Apater for the View Pager
         */
        viewpager_for_tabs.setAdapter(new MyAdapter(getChildFragmentManager()));
        viewpager_for_tabs.setOffscreenPageLimit(5);

        /**
         * Now , this is a workaround ,
         * The setupWithViewPager dose't works without the runnable .
         * Maybe a Support Library Bug .
         */

        viewpager_for_tabs.setOnPageChangeListener(myOnPageChangeListener);
        bottom_tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateBottomTabTextViews(bottom_tv1, R.drawable.d1);
                viewpager_for_tabs.setCurrentItem(0, false);
            }
        });
        bottom_tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateBottomTabTextViews(bottom_tv2, R.drawable.d2);
                viewpager_for_tabs.setCurrentItem(1, false);
            }
        });
        bottom_tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateBottomTabTextViews(bottom_tv3, R.drawable.d3);
                viewpager_for_tabs.setCurrentItem(2, false);
            }
        });
        bottom_tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateBottomTabTextViews(bottom_tv4, R.drawable.d4);
                viewpager_for_tabs.setCurrentItem(3, false);
            }
        });
        bottom_tv5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateBottomTabTextViews(bottom_tv5, R.drawable.d5);
                viewpager_for_tabs.setCurrentItem(4, false);
            }
        });

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewpager_for_tabs);


            }
        });

        return x;

And in adapter getitem Methods

@Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                        return new fragment();

                case 1:
                    return frag2.getInstance();
                case 2:
                   return frag3.getInstance();
                case 3:
                   return frag4.getInstance();
                case 4:
                    return frag5.getInstance();
            }
            return null;
        }

In this was i just have to pragmatically call Button onClick using performClick() method for views and it will automatically replace my fragment.

Better solution would be to change current item of viewPager Have a look at this solution pager.setCurrentItem( num )

If you want to show different fragments on a single tab of viewPager then apply Boolean condition in getItems() method of your fragment adapter. then call viewPager.setCurrentItem(int) function.

Make sure your Fragment adapter extends FragmentStatePagerAdapter class

Community
  • 1
  • 1
Muhammad Usman
  • 200
  • 1
  • 14
  • i am getting if user logged in or not from shared preference, it works fine if i reload the app, but while still using the app it retains its initial fragment when user is logged in, which is a really bad user experience. – SimpuMind Aug 15 '16 at 13:50
  • There must be some code that will check is user logged in and doing some task. In that piece of code you can replace your fragment like this http://stackoverflow.com/a/18940937/6634982. Anf if you are using viewpager you use setPage function – Muhammad Usman Aug 15 '16 at 13:56
  • I have a code that check if user is logged in, and i am using view pager with three tabs, i only want to replace the first tab with any of two different fragment pending on if the user is logged in or not. since you already have the idea the issue am having, how can i solve it. Thanks. – SimpuMind Aug 15 '16 at 14:59