Android : FragmentPagerAdapter don't saved the

2019-06-07 11:30发布

问题:

I have a problem with the FragmentPagerAdapter .

I can not save the state of the Fragment and then there is the view that within the Fragment . Whenever I use the swipe left and right , the Fragment is recreated by overriding the method getItem ( int position ) in the static class that extends the FragmentPagerAdapter .

 public static class GraphicsCollectionPagerAdapter extends FragmentPagerAdapter {

    final int NUM_ITEMS = 3; // number of tabs

    public GraphicsCollectionPagerAdapter(FragmentManager fm) {

        super(fm);
        fragmentList = new AnalyzeFragmentPageListWithDate();
        fragment1 = new  AnalyzeFragmentPage1();
        fragment2 = new  AnalyzeFragmentPage2();
    }

    @Override
    public Fragment getItem(int position) {
        //Log.i(TAG, "getItem() -> New fragment at position " + position);


        switch (position) {

        case 0:         
            return fragmentList;
       case 1:
            return fragment1;
       case 2:
             return fragment2;
        }
        return null;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {

         case 0:
            return "Fragm1";
        case 1:
            return "Fragm2";
        case 2:
            return "Fragm3";

    }
        return "OBJECT " + (position + 1);
    }


}

Within the method OnCreateView of each instance of the Fragment there are several steps to the SQLite database and this causes a saturation of the Database Connection Pool.

The warning found whenever change dynamically fragment is: "W / SQLiteConnectionPool (1111 ) : A SQLiteConnection object for database ' / data / data / com.myapp / databases / mydb ' was leaked ! Please fix your application to end transactions in progress properly and to close the database When it is no longer needed . "

I already tried to use the FragmentStatePagerAdapter without success.

Could you kindly tell me how to proceed ? I do not want the Fragment is regenerated each time, causing problems to the database. Have you got an example for save Fragment/View sate?

I have not found any suggestion for now . thank you very much

回答1:

use setOffscreenPageLimit property for your ViewPager object.

ViewPager pager = (ViewPager) findViewById(R.id.viewPagerId);
pager.setOffscreenPageLimit(2);


回答2:

First of all create a new instance of Fragment every time the getItem() is called

@Override
public Fragment getItem(int position) {
    switch (position) {
    case 0:         
        return new FragmentList();
    case 1:
        return new  AnalyzeFragmentPage1();
    case 2:
         return new  AnalyzeFragmentPage2();
    }
    return null;
}

Note that FragmentPagerAdapter does not work exactly like normal Adapter meaning it does NOT use an empty vessel Fragment and then refills it with different data.

Instead it creates a new Fragment whenever it is needed. So you should pass data (if any) when the Fragment is created. Please study the example in the Android docs.

In a real time projects instead of passing current position to a new Fragment you could pass the actual ID of the entry that a Fragment should refer to.