I have an activity with 3 fragments (A, B, C). Fragment A consists of a ViewPager
with 2 ListFragments
. The user can tap on an item in any of the listfragments and by doing so, goes to fragment B.
In fragment A I do:
@Override public void onAttach(Activity activity) { super.onAttach(activity); pagerAdapter = new PagerAdapter(getActivity().getSupportFragmentManager()); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmentA, container, false); vpPager = (ViewPager)view.findViewById(R.id.vpPager); vpPager.setOffscreenPageLimit(2); vpPager.setAdapter(pagerAdapter); vpPager.addOnPageChangeListener(this); return view; }
And the PagerAdapter
is as follows:
private class PagerAdapter extends FragmentPagerAdapter { private final ListFragment1 lf1 = ListFragment1 .newInstance(); private final ListFragment2 lf2 = ListFragment2 .newInstance(); public PagerAdapter(android.support.v4.app.FragmentManager fm) { super(fm); } @Override public android.support.v4.app.Fragment getItem(int position) { switch (position) { case 0: return lf1; case 1: return lf2; default: return null; } } }
The first time the activity is shown, the viewpager list fragments are displayed correctly.
The 2 viewpager fragments load data from a db, and I do this only once (when the fragments are created).
The user can tap on an item and fragment B is displayed. If the user presses Back, fragment A is shown. However the list fragments are not shown (already an instance of them still exists).
Could it be that the view has been destroyed, even though instances exist?
What is wrong here? Is there a better approach?
EDIT
If I use newInstance
in the pager adapter, I get an IllegalStateException: not attached to activity
. This is because I start an async task as follows:
@Override public void onPageSelected(int position) { Fragment fragment = pagerAdapter.getItem(position); if (fragment instanceof IPagedFragment) { ((IPagedFragment) fragment).onShown(); } }
And onShown
is:
@Override public void onShown() { myTask= new MyTask(); myTask.execute((Void)null); }
When can I start the task so that I can be 100% sure that the fragment is attached to the activity and that the view has been created (I need to get listview, etc. from the layout).
fragment:fragment:1.1. 0 you can just use onPause and onResume callbacks to determine which fragment is currently visible for the user. onResume callback is called when fragment became visible and onPause when it stops to be visible.
ViewPager save Fragment in FragmentManager with particular tags, So We can get ViewPager's fragment by FragmentManager class by providing tag. And ViewPager provide tag to each fragment by following syntax : Fragment page = getSupportFragmentManager(). findFragmentByTag("android:switcher:" + R.
Using an OnPageChangeListener in your View Pager, you can detect which fragment is currently shown in the View Pager. You'll then need to check which fragment is shown and then call a method on that fragment's class to start any sounds for example that you don't want to start until the fragment is the fragment in view.
Fragment is added and its layout is inflated. Fragment is active and visible.
You have to use ChildFragmentManager
like below.
@Override public void onAttach(Activity activity) { super.onAttach(activity); pagerAdapter = new PagerAdapter(getChildFragmentManager()); //here used child fragment manager } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmentA, container, false); vpPager = (ViewPager)view.findViewById(R.id.vpPager); vpPager.setOffscreenPageLimit(2); vpPager.setAdapter(pagerAdapter); vpPager.addOnPageChangeListener(this); return view; }
It works like charm in my code with viewpager and fragment.
Just now I solved it after struggling for whole day, by using getChildFragmentManager()
pass this as a parameter to the pagerAdapter. and it will work.
while using pagerAdapter in fragment use :
PagerAdapter adapter = new PagerAdapter(getChildFragmentManager());
and in case of activity use getFragmentManager()
PagerAdapter adapter = new PagerAdapter(getFragmentManager());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With