Skip to content

Commit

Permalink
Save view pager state on orientation change
Browse files Browse the repository at this point in the history
  • Loading branch information
daneren2005 committed May 16, 2013
1 parent b4fcab6 commit 40a18dc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,21 @@ protected void done(Void result) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public void finish() {

@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;
Expand All @@ -106,13 +106,14 @@ public void onSaveInstanceState(Bundle savedInstanceState) {
}
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();
Expand All @@ -126,6 +127,9 @@ public void onRestoreInstanceState(Bundle savedInstanceState) {
backStack.add(frag);
}
recreateSpinner();
} else {
pagerAdapter.onRestoreInstanceState(savedInstanceState);
super.onRestoreInstanceState(savedInstanceState);
}
}

Expand Down Expand Up @@ -538,6 +542,10 @@ public boolean onBackPressed() {
}

private void recreateSpinner() {
if(frags.isEmpty()) {
return;
}

List fragStack = (List)frags.get(currentPosition);
if(fragStack.size() > 1) {
spinnerAdapter.clear();
Expand All @@ -560,6 +568,43 @@ public void invalidate() {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 40a18dc

Please sign in to comment.