ViewPager inside Fragment loses content when returning to it
Asked Answered
N

3

9

I am implementing tabhost using FragmentActivity , under the first tab i load a welcome fragment that contains a Viewpager and CirclePageIndIcator, I need to load 4 views in side that WelcomeFragment

this is my WelcomeFragment

public class WelcomeFragment extends Fragment
{
     PageAdapter mAdapter;
        ViewPager mPager;
        CirclePageIndicator mIndicator;
        List<Fragment> fragments;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_welcome_layout,null);
        mPager=(ViewPager)view.findViewById(R.id.myViewPager);
        mIndicator=(CirclePageIndicator)view.findViewById(R.id.circlePageIndicator1);
        fragments=new ArrayList<Fragment>();
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Fragment fragment;
        for(int i=0;i<4;i++)
        {
            fragment= Fragment.instantiate(getActivity(), TipsFragment.class.getName());
            fragments.add(fragment);
        }
            mAdapter = new PageAdapter(getFragmentManager(),fragments);
            mPager.setAdapter(mAdapter);
        mIndicator.setViewPager(mPager);
    }
}

and this is my page adapter

public class PageAdapter extends FragmentPagerAdapter implements Serializable{

    List<Fragment> fragments;
    public PageAdapter(FragmentManager fm,List<Fragment> frags) {
        super(fm);
        fragments = frags;

    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        return TipsFragment.newInstance(0, 0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 4;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        FragmentManager manager = ((Fragment) object).getFragmentManager();
        FragmentTransaction trans = manager.beginTransaction();
        trans.remove((Fragment) object);
        trans.commit();

        super.destroyItem(container, position, object);
    }

}

When I load the app first the tabFragmentActivity load successfully and the viewpager inside the welcomeFragment works fine, but when I move to 2nd tab and comes back to the 1st tab again the ViewPager doesn't load successfully.

How can i solve it?

as per the suggestion i used getChildFragmentManager but app got crash error in logcat

09-09 13:28:39.412: E/AndroidRuntime(3100): FATAL EXCEPTION: main
09-09 13:28:39.412: E/AndroidRuntime(3100): java.lang.IllegalStateException: No activity
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1492)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.os.Handler.handleCallback(Handler.java:725)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.os.Looper.loop(Looper.java:137)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at java.lang.reflect.Method.invoke(Method.java:511)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-09 13:28:39.412: E/AndroidRuntime(3100):     at dalvik.system.NativeStart.main(Native Method

)

Nidia answered 9/9, 2013 at 12:46 Comment(0)
S
14

if you are using your ViewPager in a Fragment, when you are creating a new adapter, you should use ChildFragmentManager:

getChildFragmentManager()
Sneaking answered 20/1, 2015 at 14:38 Comment(0)
T
4

Try this:

    ViewPager pager = [your viewPAger]
    pager.setOffscreenPageLimit([quantity of tabs that you are going to use]);

with that sentence (pager.setOffscreenPageLimit) you can specify how far away a tab can stand before it get erased by the ViewPager!

Cheers!

Touchback answered 3/6, 2014 at 12:52 Comment(0)
P
2

Use an AsyncTask to set the ViewPagerAdapter:

private class SetAdapterTask extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            if(mPagerAdapter != null) mViewPager.setAdapter(mPagerAdapter);
        }
    }   

Call it like this:

mAdapter = new PageAdapter(getChildFragmentManager(), fragments);
new SetAdapterTask().execute();

instead of calling:

mViewPager.setAdapter(mPagerAdapter);

And set the Adapter inside the onCreateView(...) method, not onActivityCreated(...).

Also, use getChildFragmentManager() instead of getFragmentManager().

Prance answered 9/9, 2013 at 12:52 Comment(7)
Set the adapter in the onCreateView() method. You are doing it in onActivityCreated(), which is only called once.Prance
Also, you need to use getChildFragmentManager(). I updated my answer.Prance
as your suggestion i use childFragmentManager() now when i come back from 2nd tab to 1st tab... the application crashd.... and error log also given aboveNidia
I dont see your error message. Could you pastebin the current code of your fragment and the error log. pastebin.comPrance
@user1672337 did you solve this problem? i am having the same problemUndercharge
ya...it correct for nested fragment, need to use new instance and getChildFragmentManagerAliaalias
@PhilippJahoda.. Its not working. If I click the tab 1st time its loading fine but if I switch back to other tab & come back then app is getting crashed with IllegalStateException : No ActivityPyroelectricity

© 2022 - 2024 — McMap. All rights reserved.