2

So I have a nested Fragment and view pager scenario. I have an activity that loads ParentFragment. ParentFragment has a ViewPager that has 4 fragments. Normally the view pager starts at 0. But because I want to help the user return back to their previous position, I wanted to see what happens if I use setCurrentItem(1) for example.

The viewpager does go to the 2nd tab when I call setCurrentItem(1), however, if the user triggers the onClickListener to startActivityForResult from the MyFrag2, the onActivityResult on the ParentFragment does NOT get called. However, if I use setCurrentItem(0) or don't ever call that so the view pager starts off at the first position every time, then the ParentFragment gets the onActivityResult() call which then passes it to the underlying fragments.

I thought originally the ParentFragment was cached or the Fragments in the ViewPager were cached, but I couldn't find information supporting that. Any help?

Here is the MainActivity

public class MainActivity extends FragmentActivity {
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

The following is the parent fragment

public class ParentFragment extends Fragment {
    ViewPager viewPager;
    ArrayList<Fragment> listOfFrags = new ArrayList<Fragment>();

    // other code here

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViewPager()
    }

    public void initViewPager() {
        Fragment frag1 = MyFrag1.newInstance();
        Fragment frag2 = MyFrag2.newInstance();
        Fragment frag3 = MyFrag3.newInstance();
        Fragment frag4 = MyFrag4.newInstance();
        listOfFrags.add(frag1);
        listOfFrags.add(frag2);
        listOfFrags.add(frag3);
        listOfFrags.add(frag4);

        PagerAdapter pagerAdapter = MyPagerAdapter(getChildFragmentManager(), listOfFrags);
        viewPager.setAdapter(pagerAdapter);

        // proving a point here that setting something other than 0 causes weird behaviour
        viewPager.setCurrentItem(1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        for (Fragment frag : listOfFrags) {
            frag.onActivityResult(requestCode, resultCode, data);
        }
    }
}

Here is the the MyFrag2

public class MyFrag2 extends Fragment {

    public onClickListener getButtonClickListener() {
        return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), OtherAcivity.class);
                    startActivityForResult(intent);
                }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // do stuff
    }
}
lazypig
  • 747
  • 1
  • 6
  • 22
  • first of all in the intent in `MyFrag2` you have to `new Intent(getActivity().getApplicationContext(), OtherAcivity.class);` – Kostas Drak Feb 13 '15 at 22:10
  • where are you using the method "initViewPager()" ? where is it called from? – Lena Bru Feb 13 '15 at 22:23
  • For the MyFrag2, myFrag2 has a static method called newInstance(). For the intent, all my fragments will have a context so safely assume that the context that I am using is correct. Also, the iniViewPager is assumed to be called during the onActivityCreated() method. – lazypig Feb 13 '15 at 22:38
  • the activity that you are running this viewpager fragment in, in your manifest did you change any launchmodes for it ? – Lena Bru Feb 14 '15 at 08:13
  • Hello Lena, I have only make it hardware accelerated and portrait only. The usual styling for action bar is there as well. – lazypig Feb 16 '15 at 17:21
  • First of all ViewPager instantiate next page when possible that is why you update may effect next page. Check this [answer](http://stackoverflow.com/a/29369154/1283715) – Khaled Lela Mar 31 '15 at 23:20

0 Answers0