0

I have an activity which implement ViewPager with 3 menus (Menu 1, Menu 2, Menu 3) .Here's my code:

public class MyActivity .... {

    PagerAdapter pa;
    TabLayout tabLayout;
    ViewPager vp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);

        vp = (ViewPager) findViewById(R.id.viewPager);
        pa = new PageAdapter(getSupportFragmentManager(), getApplicationContext());
        vp.setAdapter(pa);

        tabLayout = (TabLayout) findViewById(R.id.viewPagerMenu);
        tabLayout.setTabsFromPagerAdapter(pa);
        tabLayout.setupWithViewPager(vp);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                int position = vp.getCurrentItem();

                if(position == 0){
                    // I want to take the fragment object that instantiated in this position, to access variable from it's object

                }
            }
        });
    }
}   

public class Fragment1 extends Fragment ... {

    // I want to get Boolean variable from this fragment

    private Boolean toggleSomething = false;
    private Context context;
    private String pageTitle;

    public static Fragment1 newInstance(String pageTitle, Context context){
        Fragment1 frag = new Fragment1();
        frag1.setPageTitle(pageTitle);
        frag1.setContext(context);

        return frag;
    }

    public Boolean getToggleSomething(){
        return this.toggleSomething;
    }

    public void setToggleSomething(Boolean toggleSomething){
        this.toggleSomething = toggleSomething;
    }

    public String getPageTitle(){
        return this.pageTitle;
    }

    public void setPageTitle(String pageTitle){
        this.pageTitle = pageTitle;
    }

    public Context getContext(){
        return this.context;
    }

    public void setContext(Context context){
        this.context = context;
    }

}       

Here's my PageAdapter:

public class PageAdapter extends FragmentPagerAdapter {

    private static int totalMenu = 3;
    private Context context;

    public PageAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return Fragment1.newInstance("Menu 1", context);
            case 1:
                return Fragment2.newInstance("Menu 2", context);
            case 2:
                return Fragment3.newInstance("Menu 3", context);
            default:
                return null;
        }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0:
                return "Menu 1";
            case 1:
                return "Menu 2";
            case 2:
                return "Menu 3";
            default:
                return null;
        }
    }
}

My problem is:

I have a boolean inside a fragment and i want to get that value from my activity when the tab selected is the first tab. How can i do that?

I think i need to get the fragment's object then access the variable with getter, any idea to do that? or there's other approach to do that?

Any answer would be appreciated, thanks!

Stanley Giovany
  • 583
  • 1
  • 7
  • 12
  • Check this [link](http://stackoverflow.com/a/9977370/1949520)! – cylon Dec 30 '15 at 09:20
  • Yes you need to create objects of Fragments in Actvity and using that objects u can pass values in fragment creating a function in fragment and pass the value. – Rohit Heera Dec 30 '15 at 09:23

2 Answers2

0

you have to pass the bool value on item select

     @Override
        public void onPageSelected(int position) {
if(case1){ return Fragment1.newInstance("Menu 1", context,BOOL_VALUE);}}

and change your constructor to revive the boolean

public Fragment1 newInstance(String pageTitle, Context context,Boolean BOOL_VALUE){
    Fragment1 frag = new Fragment1();
    frag1.setPageTitle(pageTitle);
    frag1.setContext(context);
    frag1.setBoolValue(BOOL_VALUE);
    return frag;
}

hope this will help.

AbuQauod
  • 919
  • 14
  • 30
0

Create a public class with static variable

public class PublicValue{
   public static yourboolean = false;//default values
}

in fragment you can set the value to static variable

PublicValue.yourboolean = value_you_want use;

then use static variable

@Override
            public void onPageScrollStateChanged(int state) {
                int position = vp.getCurrentItem();

                if(position == 0){
                    // I want to take the fragment object that instantiated in this position, to access variable from it's object
                  if( PublicValue.yourboolean){

                }
            }
        });
Deepak John
  • 967
  • 1
  • 7
  • 19