From d23759f1b84dbd550c933dd2d8b9b80a431af2c6 Mon Sep 17 00:00:00 2001 From: "Andrzej J.R. Hunt" Date: Tue, 4 Sep 2012 11:08:33 +0200 Subject: [PATCH] More fragment lifecycle cleanup. Change-Id: I93a19a080cc73bfa49b04f19b6e290ff9cd1b8bc --- .../res/layout/activity_presentation.xml | 12 +++- android/sdremote/res/layout/idlayout.xml | 9 --- .../impressremote/PresentationActivity.java | 59 +++++++++++-------- .../impressremote/PresentationFragment.java | 6 +- .../impressremote/ThumbnailFragment.java | 3 +- 5 files changed, 48 insertions(+), 41 deletions(-) delete mode 100644 android/sdremote/res/layout/idlayout.xml diff --git a/android/sdremote/res/layout/activity_presentation.xml b/android/sdremote/res/layout/activity_presentation.xml index f482b3c246ef..23b615a95fe4 100644 --- a/android/sdremote/res/layout/activity_presentation.xml +++ b/android/sdremote/res/layout/activity_presentation.xml @@ -1,7 +1,13 @@ + android:layout_height="match_parent" > - + + + \ No newline at end of file diff --git a/android/sdremote/res/layout/idlayout.xml b/android/sdremote/res/layout/idlayout.xml deleted file mode 100644 index 6288cbbb7144..000000000000 --- a/android/sdremote/res/layout/idlayout.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - \ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/PresentationActivity.java b/android/sdremote/src/org/libreoffice/impressremote/PresentationActivity.java index a99b9cc869c5..767bec5a4f57 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/PresentationActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/PresentationActivity.java @@ -20,6 +20,7 @@ import android.preference.PreferenceManager; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.text.format.DateFormat; +import android.util.AttributeSet; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -40,7 +41,7 @@ public class PresentationActivity extends SherlockFragmentActivity { private FrameLayout mOuterLayout; private ThumbnailFragment mThumbnailFragment; private PresentationFragment mPresentationFragment; - private ActionBarManager mActionBarManager; + private static ActionBarManager mActionBarManager; @Override public void onCreate(Bundle savedInstanceState) { @@ -49,32 +50,22 @@ public class PresentationActivity extends SherlockFragmentActivity { bindService(new Intent(this, CommunicationService.class), mConnection, Context.BIND_IMPORTANT); - setContentView(R.layout.activity_presentation); - mOuterLayout = (FrameLayout) findViewById(R.id.framelayout); - mOuterLayout.removeAllViews(); - mLayout = new InterceptorLayout(this); - mOuterLayout.addView(mLayout); - mLayout.setId(R.id.presentation_innerFrame); - //((FrameLayout) findViewById(R.id.framelayout)).addView(mLayout); + setContentView(R.layout.activity_presentation); if (savedInstanceState == null) { - mThumbnailFragment = new ThumbnailFragment(); + mPresentationFragment = new PresentationFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); - fragmentTransaction.add(R.id.presentation_innerFrame, + fragmentTransaction.add(R.id.presentation_interceptor, mPresentationFragment, "fragment_presentation"); fragmentTransaction.commit(); } - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putBoolean("thumbnail_enabled", mThumbnailFragment.isVisible()); + mOuterLayout = (FrameLayout) findViewById(R.id.framelayout); + mLayout = (FrameLayout) findViewById(R.id.presentation_interceptor); } @Override @@ -90,8 +81,10 @@ public class PresentationActivity extends SherlockFragmentActivity { .getDefaultSharedPreferences(this); boolean aVolumeSwitching = aPref.getBoolean("option_volumeswitching", false); - boolean aRelevantFragmentVisible = mPresentationFragment.isVisible() - || mThumbnailFragment.isVisible(); + boolean aRelevantFragmentVisible = ((mPresentationFragment != null) && mPresentationFragment + .isVisible()) + || ((mThumbnailFragment != null) && mThumbnailFragment + .isVisible()); if (aVolumeSwitching && aRelevantFragmentVisible) { int action = event.getAction(); @@ -119,7 +112,9 @@ public class PresentationActivity extends SherlockFragmentActivity { mCommunicationService = ((CommunicationService.CBinder) aService) .getService(); - mThumbnailFragment.setCommunicationService(mCommunicationService); + if (mThumbnailFragment != null) + mThumbnailFragment + .setCommunicationService(mCommunicationService); } @@ -149,8 +144,10 @@ public class PresentationActivity extends SherlockFragmentActivity { startActivity(aIntent); return true; case R.id.actionbar_presentation_submenu_blank: - boolean aRelevantFragmentVisible = mPresentationFragment - .isVisible() || mThumbnailFragment.isVisible(); + boolean aRelevantFragmentVisible = (mPresentationFragment != null && mPresentationFragment + .isVisible()) + || (mThumbnailFragment != null && mThumbnailFragment + .isVisible()); if (aRelevantFragmentVisible) { BlankScreenFragment aFragment = new BlankScreenFragment( @@ -158,7 +155,7 @@ public class PresentationActivity extends SherlockFragmentActivity { FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); - ft.replace(R.id.presentation_innerFrame, aFragment); + ft.replace(R.id.presentation_interceptor, aFragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit(); @@ -372,10 +369,20 @@ public class PresentationActivity extends SherlockFragmentActivity { Timer aTimer = mCommunicationService.getSlideShow().getTimer(); // --------------------------------- ACTIONBAR BUTTONS ------------- if (aSource == mThumbnailButton) { + if (mThumbnailFragment == null) { + mThumbnailFragment = (ThumbnailFragment) getSupportFragmentManager() + .findFragmentByTag("ThumbnailFragment"); + if (mThumbnailFragment == null) { + mThumbnailFragment = new ThumbnailFragment(); + mThumbnailFragment + .setCommunicationService(mCommunicationService); + } + } if (!mThumbnailFragment.isVisible()) { FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); - ft.replace(R.id.presentation_innerFrame, mThumbnailFragment); + ft.replace(R.id.presentation_interceptor, + mThumbnailFragment, "ThumbnailFragment"); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit(); @@ -480,10 +487,10 @@ public class PresentationActivity extends SherlockFragmentActivity { * @author andy * */ - private class InterceptorLayout extends FrameLayout { + public static class InterceptorLayout extends FrameLayout { - public InterceptorLayout(Context context) { - super(context); + public InterceptorLayout(Context context, AttributeSet aAttrs) { + super(context, aAttrs); } @Override diff --git a/android/sdremote/src/org/libreoffice/impressremote/PresentationFragment.java b/android/sdremote/src/org/libreoffice/impressremote/PresentationFragment.java index 2ca4a2114d11..2d5dfacb0243 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/PresentationFragment.java +++ b/android/sdremote/src/org/libreoffice/impressremote/PresentationFragment.java @@ -59,7 +59,8 @@ public class PresentationFragment extends SherlockFragment { if (mTopView != null) { mTopView.setAdapter(new ThumbnailAdapter(mContext, mCommunicationService.getSlideShow())); - mTopView.setSelection(mCommunicationService.getSlideShow().getCurrentSlide(), true); + mTopView.setSelection(mCommunicationService.getSlideShow() + .getCurrentSlide(), true); mTopView.setOnItemSelectedListener(new ClickListener()); } @@ -75,12 +76,13 @@ public class PresentationFragment extends SherlockFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + setRetainInstance(true); getActivity().bindService( new Intent(getActivity().getApplicationContext(), CommunicationService.class), mConnection, Context.BIND_IMPORTANT); mContext = getActivity().getApplicationContext(); - container.removeAllViews(); + // container.removeAllViews(); View v = inflater.inflate(R.layout.fragment_presentation, container, false); diff --git a/android/sdremote/src/org/libreoffice/impressremote/ThumbnailFragment.java b/android/sdremote/src/org/libreoffice/impressremote/ThumbnailFragment.java index 4532177f9fde..c7731ee56e6b 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/ThumbnailFragment.java +++ b/android/sdremote/src/org/libreoffice/impressremote/ThumbnailFragment.java @@ -46,7 +46,8 @@ public class ThumbnailFragment extends SherlockFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - container.removeAllViews(); + // container.removeAllViews(); + setRetainInstance(true); View v = inflater .inflate(R.layout.fragment_thumbnail, container, false);