Skip to content

Instantly share code, notes, and snippets.

@tschellenbach
Created February 21, 2014 12:26
Show Gist options
  • Save tschellenbach/9133395 to your computer and use it in GitHub Desktop.
Save tschellenbach/9133395 to your computer and use it in GitHub Desktop.
private void openIfNotHere(Class<? extends Activity> targetClass) {
if (!getClass().isAssignableFrom(targetClass)) {
/*
* When clicking a menu item we follow Google's best practices as defined here:
* https://developer.android.com/design/patterns/navigation.html
* and
* https://developer.android.com/guide/components/tasks-and-back-stack.html
*
* The result is that when you click to
* - people and subsequently
* - shops
* - the back button
* You end up on the home activity instead of the people activity
*/
Intent intent = new Intent(this, targetClass);
// Disable animation when navigating forward
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
// Create a new task stack with just home and the menu item we're navigating to
TaskStackBuilder task = TaskStackBuilder.create(this);
if (targetClass != FashiolistaActivity.class) {
Intent homeIntent = new Intent(this, FashiolistaActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
task.addNextIntent(homeIntent);
}
task.addNextIntent(intent)
.startActivities();
// Disable animation when navigating home
overridePendingTransition(0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment