diff --git a/app/src/main/java/com/philliphsu/clock2/BaseFragment.java b/app/src/main/java/com/philliphsu/clock2/BaseFragment.java index 991007b..5cd5239 100644 --- a/app/src/main/java/com/philliphsu/clock2/BaseFragment.java +++ b/app/src/main/java/com/philliphsu/clock2/BaseFragment.java @@ -39,4 +39,15 @@ public abstract class BaseFragment extends Fragment { super.onDestroyView(); ButterKnife.unbind(this); // Only for fragments! } + + /** + * Callback invoked when this Fragment is part of a ViewPager and it has been + * selected, as indicated by {@link android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int) + * onPageSelected(int)}. + */ + public void onPageSelected() { + // TODO: Consider making this abstract. The reason it wasn't abstract in the first place + // is not all Fragments in our ViewPager need to do things upon being selected. As such, + // those Fragments' classes would just end up stubbing this implementation. + } } diff --git a/app/src/main/java/com/philliphsu/clock2/MainActivity.java b/app/src/main/java/com/philliphsu/clock2/MainActivity.java index e5c977b..e01b631 100644 --- a/app/src/main/java/com/philliphsu/clock2/MainActivity.java +++ b/app/src/main/java/com/philliphsu/clock2/MainActivity.java @@ -122,6 +122,10 @@ public class MainActivity extends BaseActivity { if (position < mSectionsPagerAdapter.getCount() - 1) { mFab.setImageDrawable(mAddItemDrawable); } + Fragment f = mSectionsPagerAdapter.getFragment(mViewPager.getCurrentItem()); + if (f instanceof BaseFragment) { + ((BaseFragment) f).onPageSelected(); + } } // @Override // public void onPageScrollStateChanged(int state) { diff --git a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java index e59a804..d85e0d9 100644 --- a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java +++ b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java @@ -99,23 +99,10 @@ public class StopwatchFragment extends RecyclerViewFragment< } // Hides the mini fabs prematurely, so when we actually display this tab // they won't show even briefly at all before hiding. - updateButtonControls(); +// updateButtonControls(); return view; } - @Override - public void onResume() { - super.onResume(); - // FAB icon change won't happen when selecting this tab from multiple tabs away, - // because isResumed() and isVisible() both return false, respectively from - // setUserVisibleHint() and from updateButtonControls() when called by onCreateView(). - // TODO: Since this is only called to change the FAB icon, just allow duplicate - // code and manipulate the FAB directly. - // TODO: This has the side effect of prematurely changing the icon for the - // page directly before this page. - updateButtonControls(); - } - /** * If the user navigates away, this is the furthest point in the lifecycle * this Fragment gets to. Here, the view hierarchy returned from onCreateView() @@ -212,26 +199,8 @@ public class StopwatchFragment extends RecyclerViewFragment< } @Override - public void setUserVisibleHint(boolean isVisibleToUser) { - super.setUserVisibleHint(isVisibleToUser); - // We get called multiple times, even when we're not yet visible. - // This can be called before onCreateView() so widgets could be null, esp. if you had - // navigated more than one page away before returning here. That means onDestroyView() - // was called previously. - // We will get called again when we actually have this page selected, and by that time - // onCreateView() will have been called. Wait until we're resumed to call through. - if (isVisibleToUser && isResumed()) { - Log.d(TAG, "setUserVisibleHint called thru"); - // At this point, the only thing this does is change the fab icon - // TODO: allow duplicate code and manipulate the fab icon directly? - // TODO: There is noticeable latency between showing this tab and - // changing the icon. Consider writing a callback for this Fragment - // that MainActivity can call in its onPageChangeListener. We don't merely - // want to call such a callback in onPageSelected, because that is fired - // when we reach an idle state, so we'd experience the same latency issue. - // Rather, we should animate the icon change during onPageScrolled. - updateButtonControls(); - } + public void onPageSelected() { + updateButtonControls(); } @Nullable @@ -296,8 +265,9 @@ public class StopwatchFragment extends RecyclerViewFragment< int vis = started ? View.VISIBLE : View.INVISIBLE; mNewLapButton.setVisibility(vis); mStopButton.setVisibility(vis); + // TODO: We no longer call this method in lifecycle events where we aren't actually + // resumed/visible, so we can remove this check. if (isVisible()) { // avoid changing the icon prematurely, esp. when we're not on this tab - Log.d(TAG, "Fab icon changing"); mActivityFab.get().setImageDrawable(mChronometer.isRunning() ? mPauseDrawable : mStartDrawable); } } diff --git a/app/src/main/res/layout/fragment_stopwatch.xml b/app/src/main/res/layout/fragment_stopwatch.xml index 6363ade..3b064db 100644 --- a/app/src/main/res/layout/fragment_stopwatch.xml +++ b/app/src/main/res/layout/fragment_stopwatch.xml @@ -46,6 +46,7 @@ android:layout_height="wrap_content" android:src="@drawable/ic_half_day_1_black_24dp" android:tint="@android:color/white" + android:visibility="invisible" app:layout_columnWeight="1" app:layout_gravity="center" app:fabSize="mini" @@ -57,6 +58,7 @@ android:layout_height="wrap_content" android:src="@drawable/ic_stop_24dp" android:tint="@android:color/white" + android:visibility="invisible" app:layout_columnWeight="1" app:layout_gravity="center" app:fabSize="mini"