0

In my app, I am using FragmentStatePagerAdapter with a ViewPager.

All is working well except that when I am coming from background and the memory was cleaned.

In that case, some of the fragments have disappeared. The disappeared fragments are not visible but do take the hole screen. I can't see anything.

What could cause this?

Thanks!

mort
  • 12,988
  • 14
  • 52
  • 97
roiberg
  • 13,629
  • 12
  • 60
  • 91
  • When you using FragmentStatePagerAdapter the memory is clear, use fragmentPagerAdpter – Renjith Krishnan Dec 16 '13 at 08:50
  • 1
    Can you please explain? I didn't get what you are saying.. – roiberg Dec 16 '13 at 08:51
  • Just look this link http://stackoverflow.com/questions/18747975/difference-between-fragmentpageradapter-and-fragmentstatepageradapter – Renjith Krishnan Dec 16 '13 at 08:54
  • Renjith: I also tried that and it didn't work... – roiberg Dec 16 '13 at 08:54
  • Please check "Don't keep activities" in debug settings on your device, does this make the behaviour look more deterministic? – Michael Dec 18 '13 at 10:30
  • It's just recreating the problem without the need the "clear" the memory my self. – roiberg Dec 18 '13 at 10:36
  • Can you post the code of your FragmentStatePagerAdapter ? – Andros Dec 18 '13 at 16:43
  • @roiberg Can you post your code? – GrIsHu Dec 23 '13 at 05:25
  • Without code its difficult to figure out where exactly the problem is. But to start with you can put break point in @Override public Fragment getItem(int position) { //Your code here. } And see if the items are setting properly. – Tabrej Khan Dec 23 '13 at 15:49
  • @roiberg: if your viewpager doesn't contains lots of pages then you should go for `FragmentPagerAdapter` . have a look at [this article](http://www.truiton.com/2013/06/android-fragmentpageradapter-vs-fragmentstatepageradapter/).it contains the difference between those two adapters as well as the tutorials for both. – Mehul Joisar Dec 25 '13 at 04:39

3 Answers3

0

Possible solution, given the poor problem definition:

The Fragment is being redisplayed after the Activity was recreated. Therefore, the widgets in the view are linked to the previous Activity, and you are not correctly re-inflating the view and re-wiring the widgets (redoing the findByID() commands) in onCreateView().

Any time the Activity is recreated, all Fragments must redo these things in onCreateView().

Rick Falck
  • 1,778
  • 3
  • 15
  • 19
0

In onCreate try setting setRetainInstance(true); in the Fragments.

Ali
  • 12,354
  • 9
  • 54
  • 83
0

I'm using this code... test this on another file (TestViewPager.java maybe) to see if it work and maybe adapt to your code.

For me... it works like magic :P but... I'm newbie on Android, so maybe you need another code solution (sorry I speak spanish :P)

public class Home_ViewPager extends ActionBarActivity
implements ActionBar.TabListener {

    funcionPagerAdapter miPagerAdapter;
    ViewPager miViewPager;

    public void onCreate(Bundle EstadoInstanciaSalvada){
        super.onCreate(EstadoInstanciaSalvada);
        setContentView(R.layout.home_viewpager);

        miPagerAdapter = new funcionPagerAdapter(getSupportFragmentManager());
        miViewPager = (ViewPager) findViewById(R.id.vpContenedor);
        miViewPager.setAdapter(miPagerAdapter);

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        //Set a Tab when a Fragment is selected
        miViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        actionBar.addTab(actionBar.newTab().setText(R.string.tab1_title).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.tab2_title).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.tab3_title).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.tab4_title).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.tab5_title).setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.tab6_title).setTabListener(this));
        miViewPager.setCurrentItem(1);
    }


    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    }

    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
        miViewPager.setCurrentItem(arg0.getPosition());
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    }

    public static class funcionPagerAdapter extends FragmentPagerAdapter{
        public funcionPagerAdapter(FragmentManager fm){
            super(fm);
        }

        @Override
        public Fragment getItem(int itemCapturado) {
            switch(itemCapturado){
            case 0: return new fragment_1();
            case 1: return new fragment_2();
            case 2: return new fragment_3();
            case 3: return new fragment_4();
            case 4: return new fragment_5();
            case 5: return new fragment_6();
            default:
                Fragment miFragmento = new fragment_1();
                return miFragmento;
            }
        }

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

    public static class fragment_1 extends ListFragment {
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            String[] arrayDeValores = new String[] { "yourItem1", "yourItem2", "yourItem3", "yourItem4", "yourItem5", "yourItem6"};

            setListAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, arrayDeValores));
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // Do something with the data
        }
    }

    //Others Fragments Here...

}
arickdev
  • 171
  • 2
  • 12