From 40a18dcc63106292d714705fcb88e2713f17e5a7 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Wed, 15 May 2013 19:03:32 -0700 Subject: Save view pager state on orientation change --- .../daneren2005/dsub/activity/MainActivity.java | 16 +++---- .../dsub/activity/SubsonicActivity.java | 49 +++++++++++++++++++++- .../github/daneren2005/dsub/util/Constants.java | 2 + 3 files changed, 57 insertions(+), 10 deletions(-) (limited to 'subsonic-android/src') diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java index 2f904454..8ca0fdac 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java @@ -149,21 +149,21 @@ public class MainActivity extends SubsonicActivity { addTab("Library", SelectArtistFragment.class, null); addTab("Playlists", SelectPlaylistFragment.class, null); - ChangeLog changeLog = new ChangeLog(this, Util.getPreferences(this)); - if(changeLog.isFirstRun()) { - changeLog.getLogDialog().show(); - } + getSupportActionBar().setDisplayHomeAsUpEnabled(false); + getSupportActionBar().setHomeButtonEnabled(false); + getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); } @Override protected void onPostCreate(Bundle bundle) { super.onPostCreate(bundle); - - getSupportActionBar().setDisplayHomeAsUpEnabled(false); - getSupportActionBar().setHomeButtonEnabled(false); - getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); showInfoDialog(); + + ChangeLog changeLog = new ChangeLog(this, Util.getPreferences(this)); + if(changeLog.isFirstRun()) { + changeLog.getLogDialog().show(); + } } @Override diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java index ffb7d362..2a0c793e 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java @@ -95,8 +95,8 @@ public class SubsonicActivity extends SherlockFragmentActivity implements OnItem @Override public void onSaveInstanceState(Bundle savedInstanceState) { + super.onSaveInstanceState(savedInstanceState); if(viewPager == null) { - super.onSaveInstanceState(savedInstanceState); int[] ids = new int[backStack.size() + 1]; ids[0] = currentFragment.getSupportTag(); int i = 1; @@ -106,13 +106,14 @@ public class SubsonicActivity extends SherlockFragmentActivity implements OnItem } savedInstanceState.putIntArray(Constants.MAIN_BACK_STACK, ids); savedInstanceState.putInt(Constants.MAIN_BACK_STACK_SIZE, backStack.size() + 1); + } else { + pagerAdapter.onSaveInstanceState(savedInstanceState); } } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { if(viewPager == null) { super.onRestoreInstanceState(savedInstanceState); - int size = savedInstanceState.getInt(Constants.MAIN_BACK_STACK_SIZE); int[] ids = savedInstanceState.getIntArray(Constants.MAIN_BACK_STACK); FragmentManager fm = getSupportFragmentManager(); @@ -126,6 +127,9 @@ public class SubsonicActivity extends SherlockFragmentActivity implements OnItem backStack.add(frag); } recreateSpinner(); + } else { + pagerAdapter.onRestoreInstanceState(savedInstanceState); + super.onRestoreInstanceState(savedInstanceState); } } @@ -538,6 +542,10 @@ public class SubsonicActivity extends SherlockFragmentActivity implements OnItem } private void recreateSpinner() { + if(frags.isEmpty()) { + return; + } + List fragStack = (List)frags.get(currentPosition); if(fragStack.size() > 1) { spinnerAdapter.clear(); @@ -560,6 +568,43 @@ public class SubsonicActivity extends SherlockFragmentActivity implements OnItem frag.invalidate(); } } + + public void onSaveInstanceState(Bundle savedInstanceState) { + for(int i = 0; i < frags.size(); i++) { + List fragStack = (List)frags.get(i); + String[] ids = new String[fragStack.size()]; + + for(int j = 0; j < fragStack.size(); j++) { + ids[j] = ((SubsonicFragment)fragStack.get(j)).getTag(); + } + savedInstanceState.putStringArray(Constants.MAIN_BACK_STACK + i, ids); + savedInstanceState.putInt(Constants.MAIN_BACK_STACK_SIZE + i, fragStack.size()); + } + savedInstanceState.putInt(Constants.MAIN_BACK_STACK_TABS, frags.size()); + savedInstanceState.putInt(Constants.MAIN_BACK_STACK_POSITION, currentPosition); + } + + public void onRestoreInstanceState(Bundle savedInstanceState) { + int tabCount = savedInstanceState.getInt(Constants.MAIN_BACK_STACK_TABS); + FragmentManager fm = activity.getSupportFragmentManager(); + for(int i = 0; i < tabCount; i++) { + int stackSize = savedInstanceState.getInt(Constants.MAIN_BACK_STACK_SIZE + i); + String[] ids = savedInstanceState.getStringArray(Constants.MAIN_BACK_STACK + i); + List fragStack = new ArrayList(); + + for(int j = 0; j < stackSize; j++) { + SubsonicFragment frag = (SubsonicFragment)fm.findFragmentByTag(ids[j]); + fragStack.add(frag); + } + + frags.add(i, fragStack); + } + currentPosition = savedInstanceState.getInt(Constants.MAIN_BACK_STACK_POSITION); + List fragStack = (List)frags.get(currentPosition); + currentFragment = (SubsonicFragment)fragStack.get(fragStack.size() - 1); + currentFragment.setPrimaryFragment(true); + activity.invalidateOptionsMenu(); + } private class TabInfo { public final Class fragmentClass; diff --git a/subsonic-android/src/github/daneren2005/dsub/util/Constants.java b/subsonic-android/src/github/daneren2005/dsub/util/Constants.java index 878324b7..302ececf 100644 --- a/subsonic-android/src/github/daneren2005/dsub/util/Constants.java +++ b/subsonic-android/src/github/daneren2005/dsub/util/Constants.java @@ -96,6 +96,8 @@ public final class Constants { public static final String MAIN_BACK_STACK = "backStackIds"; public static final String MAIN_BACK_STACK_SIZE = "backStackIdsSize"; + public static final String MAIN_BACK_STACK_TABS = "backStackTabs"; + public static final String MAIN_BACK_STACK_POSITION = "backStackPosition"; // Name of the preferences file. public static final String PREFERENCES_FILE_NAME = "github.daneren2005.dsub_preferences"; -- cgit v1.2.3