Skip to content

Instantly share code, notes, and snippets.

@code-twister
Created September 6, 2017 12:37
Show Gist options
  • Save code-twister/f05aaf4907b2d9f28029c56e8b15fab1 to your computer and use it in GitHub Desktop.
Save code-twister/f05aaf4907b2d9f28029c56e8b15fab1 to your computer and use it in GitHub Desktop.
// Simple stack implementation, works as expected every time
private Stack<NavItem> navigationStack = new Stack<>();
// ...
// Navigating to a new screen/item in the stack
navigationStack.push(new NavItem(viewToBeAdded.getClass(), args));
// ...
// Navigating back using the stack
if (navigationStack.size() > 1) {
navigationStack.pop();
NavItem navItem = navigationStack.peek();
CustomView customView = (CustomView) navItem.viewClass.getConstructor(Context.class).newInstance(this);
container.removeAllViews();
customView.setArgs(navItem.args);
container.addView(customView);
} else {
super.onBackPressed();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment