Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Fragment state in ViewPager

I tried out the sample code from the API and it didn't really work so I implemented my own:

FragmentPagerSupport

public class FragmentPagerSupport extends FragmentActivity {  static final int NUM_ITEMS = 10; MyAdapter mAdapter; ViewPager mPager;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      mAdapter = new MyAdapter(getSupportFragmentManager());     Log.i("Pager", "mAdapter = " + mAdapter.toString());      mPager = (ViewPager)findViewById(R.id.pager);     if (mPager == null)         Log.i("Pager", "mPager = null");     else          Log.i("Pager", "mPager = " + mPager.toString());      Log.i("Pager", "Setting Pager Adapter");     mPager.setAdapter(mAdapter); }  public static class MyAdapter extends FragmentPagerAdapter {     public MyAdapter(FragmentManager fm) {         super(fm);         Log.i("Pager", "MyAdapter constructor");     }      @Override     public int getCount() {         Log.i("Pager", "MyAdapter.getCount()");         return NUM_ITEMS;                 }      @Override     public Fragment getItem(int position) {         Log.i("Pager", "MyAdapter.getItem()");          return TestFragment.newInstance(position);     } }  public static class TestFragment extends Fragment {      public static TestFragment newInstance(int position) {         Log.i("Pager", "TestFragment.newInstance()");          TestFragment fragment = new TestFragment();          Bundle args = new Bundle();         args.putInt("position", position);         fragment.setArguments(args);          return fragment;     }     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,             Bundle savedInstanceState) {         // TODO Auto-generated method stub         Log.i("Pager", "TestFragment.onCreateView()");          LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.fragment_item, null);         int position = getArguments().getInt("position");          TextView tv = (TextView)layout.findViewById(R.id.text);         tv.setText("Fragment # " + position);         tv.setTextColor(Color.WHITE);         tv.setTextSize(30);          return layout;     }  } 

}

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     >     <android.support.v4.view.ViewPager     android:id="@+id/pager"     android:layout_width="match_parent"     android:layout_height="match_parent"     /> </LinearLayout> 

fragment_item.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     >     <TextView       android:id="@+id/text"     android:layout_width="match_parent"      android:layout_height="match_parent"     android:gravity="center"      android:text="@string/hello"     /> </LinearLayout> 

My question is, instead of creating a new Fragment instance each time the user swipes left and right, how do I save a fragment's state (to some data structure) and then restore it?

The API demo doesn't seem to have any state information saving code at all.

like image 642
Android Noob Avatar asked Jul 26 '11 18:07

Android Noob


People also ask

How do you save a fragment state?

Various Android system operations can affect the state of your fragment. To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.

Is ViewPager deprecated?

This function is deprecated.

When should I use ViewPager?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.


2 Answers

Use ViewPager.setOffscreenPageLimit() in FragmentActivity

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

See more at How do I tell my custom FragmentPagerAdapter to stop destroying my fragments?

like image 186
Thein Avatar answered Sep 21 '22 15:09

Thein


just override this method in FragmentpagerAdapter

@Override         public void destroyItem(ViewGroup container, int position, Object object) {             // TODO Auto-generated method stub             super.destroyItem(ViewGroup container, int position, Object object);         } 

remove super.destroyItem(ViewGroup container, int position, Object object);

from your code

like image 43
Jinu Avatar answered Sep 23 '22 15:09

Jinu