JakeWharton / ViewPagerIndicator

Paging indicator widgets compatible with the ViewPager from the Android Support Library and ActionBarSherlock.
http://viewpagerindicator.com
10.14k stars 4.01k forks source link

After rotation returned Data gets deleted. #137

Closed rashiq closed 11 years ago

rashiq commented 11 years ago

Hey, Everytime I rotate my Application all Data gets deleted. For example in my public Fragment getItem(int position) I return a certain value for every Item Fragment in the Viewpager, but when I rotete the screen, the value is gone. I hope you could understand what I mean. Here is my code:

public class Activity_result extends SherlockFragmentActivity {

private static final String[] CONTENT = new String[] { "GRUPPE A", "GRUPPE B", "GRUPPE C", "GRUPPE D", "GRUPPE E"};

TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tabs_viewpager);

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);

}

class TestFragmentAdapter extends FragmentPagerAdapter {        
    private int mCount = CONTENT.length;

    public TestFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
        case 0:
            return Fragment_results.newInstance("hi"); 
        case 1:
            return Fragment_results.newInstance(String.valueOf(1));
        case 2:
            return Fragment_results.newInstance(String.valueOf(2));
        case 3:
            return Fragment_results.newInstance(String.valueOf(3));
        case 4:
            return Fragment_results.newInstance(String.valueOf(4));
        case 5:
            return Fragment_results.newInstance(String.valueOf(5));
        case 6:
            return Fragment_results.newInstance(String.valueOf(6));

        }
        return Fragment_results.newInstance(String.valueOf(10)); 
    }

   public String getstring(int i){

       return "hi"; 
   }
    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position];
    }
}

 }

   public class Fragment_results extends SherlockFragment {
    private String mContent = "???";

    public static Fragment_results newInstance(String text) {
        Fragment_results fragment = new Fragment_results();
        fragment.mContent = text;
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_result, null);
        String text = "Tab Nr: " + mContent;
        ((TextView)view.findViewById(R.id.text)).setText(text);

        return view;
    }
}

Now before rotating everything works well, so for the first fragment in the Viewpager the TextView shows "hi", for the second a 2, for the third a 3 etc... But when you rotate the screen, you'll just see the default value of the String mContent (which is "???" ). How can I prevent the Data I returned to be deleted?

Here are screenshots:

http://imgur.com/a/xpkdz

LegACy99 commented 11 years ago

It's an Android behavior where on device rotation, onCreate (and onCreateView) is called, effectively resetting your stuff back to when it's initialized. To counter it, simply override onSaveInstanceState function and save the data that need to persist to the state bundle. This bundle will be returned as a parameter variable in the onCreate/onCreateView function, where you can check and use the values inside that bundle to restore your persisting data.