From 261b7634df2abb55233d9470856bacedfe632801 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Tue, 4 Sep 2018 09:03:39 -0400 Subject: [PATCH] Do not add Fragments to backstack in FragmentNavigationActivity --- .../fragment/FragmentNavigationActivity.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/fragment/FragmentNavigationActivity.java b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/fragment/FragmentNavigationActivity.java index 67b6350dffc..867f4e9fa7b 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/fragment/FragmentNavigationActivity.java +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/activity/navigationui/fragment/FragmentNavigationActivity.java @@ -7,6 +7,7 @@ import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; +import android.view.View; import com.mapbox.services.android.navigation.testapp.R; @@ -16,6 +17,8 @@ public class FragmentNavigationActivity extends AppCompatActivity { + private static final String FAB_VISIBLE_KEY = "restart_fab_visible"; + @BindView(R.id.restart_navigation_fab) FloatingActionButton restartNavigationFab; @@ -27,9 +30,23 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { initializeNavigationViewFragment(savedInstanceState); } + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(FAB_VISIBLE_KEY, restartNavigationFab.getVisibility() == View.VISIBLE); + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + boolean isVisible = savedInstanceState.getBoolean(FAB_VISIBLE_KEY); + int visibility = isVisible ? View.VISIBLE : View.INVISIBLE; + restartNavigationFab.setVisibility(visibility); + } + @OnClick(R.id.restart_navigation_fab) public void onClick(FloatingActionButton restartNavigationFab) { - replaceFragment(new NavigationFragment(), true); + replaceFragment(new NavigationFragment()); restartNavigationFab.hide(); } @@ -38,28 +55,27 @@ public void showNavigationFab() { } public void showPlaceholderFragment() { - replaceFragment(new PlaceholderFragment(), false); + replaceFragment(new PlaceholderFragment()); } private void initializeNavigationViewFragment(@Nullable Bundle savedInstanceState) { FragmentManager fragmentManager = getSupportFragmentManager(); if (savedInstanceState == null) { - FragmentTransaction ft = fragmentManager.beginTransaction(); - ft.add(R.id.navigation_fragment_frame, new NavigationFragment()).commit(); + FragmentTransaction transaction = fragmentManager.beginTransaction(); + transaction.disallowAddToBackStack(); + transaction.add(R.id.navigation_fragment_frame, new NavigationFragment()).commit(); } } - private void replaceFragment(Fragment newFragment, boolean addToBackStack) { + private void replaceFragment(Fragment newFragment) { String tag = String.valueOf(newFragment.getId()); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + transaction.disallowAddToBackStack(); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); int fadeInAnimId = android.support.v7.appcompat.R.anim.abc_fade_in; int fadeOutAnimId = android.support.v7.appcompat.R.anim.abc_fade_out; transaction.setCustomAnimations(fadeInAnimId, fadeOutAnimId, fadeInAnimId, fadeOutAnimId); transaction.replace(R.id.navigation_fragment_frame, newFragment, tag); - if (addToBackStack) { - transaction.addToBackStack(tag); - } transaction.commit(); } }