0

I have a ViewPager, defined in an Activity, and many Fragments sequentially shown in the ViewPager. In these fragments there are dynamically constructed checkboxes and radiobuttons, which the user is supposed to manipulate. On the very moment that the user swipes to the next page I need the user data to be retrieved and stored in the Application object. I can't figure out what the standard way of doing this is. Since there are many Fragments I opted for using the FragmentStatePagerAdapter. Any help would be welcome, thanks in advance!

Update-1: I do have this:

        pageAdapter = new MyPageAdapter(getSupportFragmentManager());
    pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(pageAdapter);

    // detects viewpager page change
    pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            Log.i("TAG", "onPageSelected");
            int index = pager.getCurrentItem();
            MyPageAdapter adapter = ((MyPageAdapter) pager.getAdapter());
            QuestionFragment fragment = (QuestionFragment) adapter.getItem(position);
            if (fragment.rdbtn != null) {
                for (int i = 0; i < fragment.rdbtn.length; i++) {
                    if (fragment.rdbtn[i].isChecked())
                        Log.i("TAG", "checked");
                    else
                        Log.i("TAG", "not checked");
                } 
            }
            // fragment.refresh();
        }
    });

When checking the debugger, after starting up, the ViewPager instantiates Fragments 0 and 1 (standard behavior). When the user manipulates fragment-0 and swipes, the handler is indeed called but with position=1, not 0. And the public elements I want to read are null!

UPDATE-2

I notice in the debugger that the data I need is stored in adapter.mCurrentPrimaryItem. How to retrieve CurrentPrimaryItem in the code?!

jhulst
  • 353
  • 1
  • 3
  • 18

1 Answers1

0

You can implement a pageChangeListener in your activity and set this to the viewPager. Then you can have a class abstract BaseFragment extends Fragment and declare an abstract method, say, getData() that every fragment in the ViewPager extends and overrides the method. And in onPageSelected() of the activity you can access those data.

Abhishek Shukla
  • 1,242
  • 8
  • 11
  • PS: one interesting thing is to handle the case when user swipes back and forth.. – Abhishek Shukla Jun 24 '14 at 19:18
  • Hello Abhishek, I updated the question. I basically had already done what you recommended,albeit no subclass, just public members. Ow wait, I called getItem, which instantiates a new fragment! Of course the members are null. So how do I access a fragment, without instantiating it via getItem? – jhulst Jun 24 '14 at 19:34
  • The simplest hack is, create an array of fragments inside the FragmentPagerAdapter of length adapter.getCount(), and fill the array while doing getItem(). And in the activity, access the fragment from this array by respective position. – Abhishek Shukla Jun 24 '14 at 19:42
  • I understand that, but I am not sure if that is the official way. Moreover I have a large number of Fragments, so I wanted to make use of the adapter FragmentStatePagerAdapter, specifically designed to minimize memory impact. I am not sure if this proposed 'hack' contradicts the design philosophy of FragmentStatePagerAdapter. – jhulst Jun 24 '14 at 19:46