title: Home Button on action bar
body: <p>I am new to android development and was following the "<a href="http://developer.android.com/training/basics/actionbar/adding-buttons.html" rel="nofollow">Adding Action Bar Tutorial</a>"</p>
  
  <p>I was trying to get the home (back carrot) button on the action bar to enable my app to go back to the main activity.</p>
  
  <p>It is mentioned in the tutorial that this can be done without handling the up button event, if the parent is defined in the xml. </p>
  
  <p>below is my xml file</p>
  
  <pre><code>&lt;activity
      android:name="com.example.firstapp.DisplayMessageActivity"
      android:label="@string/title_activity_display_message"
      android:parentActivityName="com.example.myfirstapp.MainActivity" &gt;
      &lt;meta-data
          android:name="android.support.PARENT_ACTIVITY"
          android:value="com.example.myfirstapp.MainActivity" /&gt;
  &lt;/activity&gt;
  </code></pre>
  
  <p>but pressing the carrot button does not work. what can i be doing wrong?</p>
  
  <p>EDIT:
  adding the activity code below</p>
  
  <pre><code>public class DisplayMessageActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          /*
           * setContentView(R.layout.activity_display_message); if
           * (savedInstanceState == null) {
           * getSupportFragmentManager().beginTransaction() .add(R.id.container,
           * new PlaceholderFragment()).commit(); }
           */
  
          // Get the message from the intent
          Intent intent = getIntent();
          String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
  
          // Create the text view
          TextView textView = new TextView(this);
          textView.setTextSize(40);
          textView.setText(message);
  
          // Set the text view as the activity layout
          setContentView(textView);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          /*
           * switch (item.getItemId()) { case android.R.id.home: Intent intent =
           * new Intent(getApplicationContext(),
           * com.example.firstapp.MainActivity.class);
           * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           * startActivity(intent); return true; default: return
           * super.onOptionsItemSelected(item); }
           */
          return super.onOptionsItemSelected(item);
      }
  
      /**
       * A placeholder fragment containing a simple view.
       */
      public static class PlaceholderFragment extends Fragment {
  
          public PlaceholderFragment() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_display_message,
                      container, false);
              return rootView;
          }
      }
  
  }
  </code></pre>
  
  <p>MainActivity is below</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if (savedInstanceState == null) {
              getSupportFragmentManager().beginTransaction()
                      .add(R.id.container, new PlaceholderFragment())
                      .commit();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu items for use in the action bar
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.main, menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle presses on the action bar items
          switch (item.getItemId()) {
              case R.id.action_search:
                  // openSearch();
                  EditText temp1 = (EditText) findViewById(R.id.edit_message);
                  temp1.setText("Search");
                  return true;
              case R.id.action_settings:
                  // openSettings();
                  EditText temp2 = (EditText) findViewById(R.id.edit_message);
                  temp2.setText("Settings");
                  return true;
              case android.R.id.home:
                  NavUtils.navigateUpFromSameTask(this);
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      /** Called when the user clicks the Send button */
      public void sendMessage(View view) {
          // Do something in response to button
          Intent intent = new Intent(this, DisplayMessageActivity.class);
          EditText editText = (EditText) findViewById(R.id.edit_message);
          String message = editText.getText().toString();
          intent.putExtra(EXTRA_MESSAGE, message);
          startActivity(intent);
      }
  
      /**
       * A placeholder fragment containing a simple view.
       */
      public static class PlaceholderFragment extends Fragment {
  
          public PlaceholderFragment() {
          }
  
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_main, container, false);
              return rootView;
          }
      }
  
  }
  </code></pre>
  
  <p>Thanks</p>