Browse files

ExampleViewerActivity: Make the Toolbar's Up Button go back properly.

Override onOptionsItemSelected() so we can modify the Intent
used with navigateUpTo(), to specify FLAG_ACTVITY_CLEAR, so we
resume the parent activity instead of recreating it, just as if the
user had used the back button - which the user generally cannot
distinguish from the up button anyway.
We could instead have marked the parent activity as singleTop
in the manifest, but it seems nice to keep this parent activity
more flexible.

This should fix this bug:
  • Loading branch information...
murraycu committed Jan 8, 2015
1 parent 657b885 commit a910d80663ce38ab125223ee9e51512e4ebbe888
Showing with 31 additions and 0 deletions.
  1. +31 −0 app/src/main/java/com/murrayc/galaxyzoo/app/
@@ -22,6 +22,8 @@
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

public class ExampleViewerActivity extends BaseActivity {
@@ -65,4 +67,33 @@ protected void onCreate(final Bundle savedInstanceState) {


public boolean onOptionsItemSelected(final MenuItem item) {
// Handle presses on the action bar items
final int id = item.getItemId();

if (id == {
//The base class just uses NavUtils.navigateUpFromSameTask() but we want to make sure
//that the parent activity will be resumed instead of restarted,
//to make sure we go back to the correct help for the correct question,
//Alternatively, we could just mark QuestionHelpActivity as
//android:launchMode="singleTop" in the AndroidManifest.xml but it seems reasonable to
//use QuestionHelpActivity from other places one day.

//We can use this instead of more complex code, checking NavUtils.shouldUpRecreateTask(),
//because we know that our activities will never be opened from another app.
final Intent upIntent = NavUtils.getParentActivityIntent(this);
if (upIntent != null) {

NavUtils.navigateUpTo(this, upIntent);
return true;

return super.onOptionsItemSelected(item);

0 comments on commit a910d80

Please sign in to comment.