From 70024de8492ade279a2337b71864fb3d1284d169 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sun, 14 Apr 2013 16:47:56 -0700 Subject: Manage own backstack since Google can't design a usable one --- .../daneren2005/dsub/activity/MainActivity.java | 34 ++++++++-------------- .../dsub/activity/SubsonicActivity.java | 30 +++++++++++++++++-- .../dsub/fragments/SelectDirectoryFragment.java | 1 - .../dsub/fragments/SubsonicTabFragment.java | 8 +---- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java index 06925e8b..57423fb3 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java @@ -52,28 +52,18 @@ public class MainActivity extends SubsonicActivity { @Override public void onBackPressed() { - int backStack = getSupportFragmentManager().getBackStackEntryCount(); - int currentTab = viewPager.getCurrentItem(); - - if(backStack == 0) { - if(currentTab == 0) { - AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); - builder.setTitle(R.string.common_confirm) - .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int id) { - MainActivity.super.onBackPressed(); - } - }) - .setNegativeButton(R.string.common_cancel, null); - AlertDialog dialog = builder.create(); - dialog.show(); - } else { - viewPager.setCurrentItem(0); - } - } else { - super.onBackPressed(); - pagerAdapter.removeCurrent(); + if(pagerAdapter.onBackPressed()) { + AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); + builder.setTitle(R.string.menu_exit) + .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + MainActivity.super.onBackPressed(); + } + }) + .setNegativeButton(R.string.common_cancel, null); + AlertDialog dialog = builder.create(); + dialog.show(); } } diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java index 61a8c18c..659d53db 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java @@ -224,6 +224,7 @@ public class SubsonicActivity extends SherlockFragmentActivity { private SubsonicTabFragment currentFragment; private List tabs = new ArrayList(); private List frags = new ArrayList(); + private List ids = new ArrayList(); private int currentPosition; public TabPagerAdapter(SherlockFragmentActivity activity, ViewPager pager) { @@ -241,6 +242,7 @@ public class SubsonicActivity extends SherlockFragmentActivity { List fragStack = new ArrayList(); fragStack.add(frag); frags.add(i, fragStack); + ids.add(i, 0); if(currentFragment == null) { currentFragment = (SubsonicTabFragment) frag; currentFragment.setPrimaryFragment(true); @@ -312,7 +314,7 @@ public class SubsonicActivity extends SherlockFragmentActivity { notifyDataSetChanged(); } - public void replaceCurrent(SubsonicTabFragment fragment) { + public void replaceCurrent(SubsonicTabFragment fragment, int id) { if(currentFragment != null) { currentFragment.setPrimaryFragment(false); } @@ -322,6 +324,11 @@ public class SubsonicActivity extends SherlockFragmentActivity { currentFragment = fragment; currentFragment.setPrimaryFragment(true); activity.invalidateOptionsMenu(); + + ids.add(currentPosition, id); + FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); + trans.add(id, fragment); + trans.commit(); } public void removeCurrent() { @@ -329,11 +336,30 @@ public class SubsonicActivity extends SherlockFragmentActivity { currentFragment.setPrimaryFragment(false); } List fragStack = (List)frags.get(currentPosition); - fragStack.remove(fragStack.size() - 1); + Fragment oldFrag = (Fragment)fragStack.remove(fragStack.size() - 1); currentFragment = (SubsonicTabFragment) fragStack.get(fragStack.size() - 1); currentFragment.setPrimaryFragment(true); activity.invalidateOptionsMenu(); + + FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); + trans.remove(oldFrag); + trans.commit(); + } + + public boolean onBackPressed() { + List fragStack = (List)frags.get(currentPosition); + if(fragStack.size() > 1) { + removeCurrent(); + return false; + } else { + if(currentPosition == 0) { + return true; + } else { + viewPager.setCurrentItem(0); + return false; + } + } } private class TabInfo { diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java index 92155fe1..9e8d42e9 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java @@ -118,7 +118,6 @@ public class SelectDirectoryFragment extends LibraryFunctionsFragment implements } else { menuInflater.inflate(R.menu.select_album, menu); } - hideButtons = false; } else { if(Util.isOffline(context)) { menuInflater.inflate(R.menu.select_song_offline, menu); diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java index 017b2c89..ee789382 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java @@ -22,7 +22,6 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; -import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.View; import android.widget.TextView; @@ -74,12 +73,7 @@ public class SubsonicTabFragment extends SherlockFragment { public void replaceFragment(SubsonicTabFragment fragment, int id) { this.setPrimaryFragment(false); fragment.setPrimaryFragment(true); - context.getPagerAdapter().replaceCurrent(fragment); - - FragmentTransaction trans = getFragmentManager().beginTransaction(); - trans.replace(id, fragment); - trans.addToBackStack(null); - trans.commit(); + context.getPagerAdapter().replaceCurrent(fragment, id); } protected void exit() { -- cgit v1.2.3