1

I am trying to save view pager state, in order to avoid creating fragments once again on orientation change. Any suggestions is highly appreciated.

I tried below stuff, but not sure how it will work...

    int pagerId = 0;

        mPager = (ViewPager) findViewById(R.id.pager);
        DialerPagerAdapter viewpageradapter = new DialerPagerAdapter(fm);
        if (savedInstanceState != null) {
            if (savedInstanceState.getInt("tab") != -1) {
               mPager.setCurrentItem(savedInstanceState.getInt("tab"));
                pagerId=savedInstanceState.getInt("pagerState");
            }
        }
        mPager.setOnPageChangeListener(ViewPagerListener);
        if (pagerId != -1 ){
            mPager.setId(pagerId);
        }else{
            pagerId=mPager.getId();
        }
        mPager.setAdapter(viewpageradapter);

in saveInstanceState I am doing below stuff

outState.putInt("pagerState" , mPager.getId() );

Thanks Venkatraman

2 Answers2

0

Do it this way

<activity
    android:name="yourActivityeThatContainsViewPager"
    android:configChanges="keyboardHidden|orientation|screenSize"
/>
Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • Not good advice in my opinion: http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation – cYrixmorten Sep 27 '13 at 11:05
  • configChanges is basically a hack and should only really be used as a last resort – Philio Sep 30 '13 at 13:12
0

Example saving string in shared preferences and retrieve it again anywhere in your app.

public class PreferencesData {

    public static void saveString(Context context, String key, String value) {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        sharedPrefs.edit().putString(key, value).commit();
    }

    public static String getString(Context context, String key, String defaultValue) {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return sharedPrefs.getString(key, defaultValue);
    }
}

Usage:

PreferencesData.saveString(context, "mynote", "Sherlock is weird");
// retrieve
String note = PreferencesData.getString(context, "mynote", "");

Use this to save the string on pause, and recreate it in onCreate, or where ever you need the information

The same method can easily be used for other simple types.

For your use case:

public class PreferencesData {

    public static void saveInt(Context context, String key, int value) {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        sharedPrefs.edit().putInt(key, value).commit();
    }

    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return sharedPrefs.getInt(key, defaultValue);
    }
}

In your code:

    mPager = (ViewPager) findViewById(R.id.pager);
    DialerPagerAdapter viewpageradapter = new DialerPagerAdapter(fm);

    if (savedInstanceState != null) {
        if (savedInstanceState.getInt("tab") != -1) {
           // this could also be saved with PreferencesData
           // but if you want the app to start at the first
           // tab when device is restarted or recreated, this is fine.
           mPager.setCurrentItem(savedInstanceState.getInt("tab")); 
        }
    }

    // defaults to 0 if first startup after install
    int pagerId = PreferencesData.getInt(this, "pagerId", 0);
    mPager.setId(pagerId);

    mPager.setOnPageChangeListener(ViewPagerListener);
    mPager.setAdapter(viewpageradapter);

And in onPause()

PreferencesData.saveInt(this, "pagerId", mPager.getId());
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33