Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not add Fragments to backstack in FragmentNavigationActivity #1256

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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();
}

Expand All @@ -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();
}
}