title: Android application restarts when opened by clicking the application icon
body: <p>I am new to the Android development world and I've built a simple "Hello World" app. First, activity requests a text. When the "Go" button is clicked, the app launches the second activity displaying the input text.</p>
  
  <p>If I click the HOME button and then click the application icon, the app launches the first activity again but if I press-hold the home button and click the icon from the "Recent apps" bar, it resumes the app where I left.</p>
  
  <p><strong>How do I avoid this?</strong></p>
  
  <p>I need my app to resume even if the launcher icon is clicked.</p>
  
  <p>MainActivity.java,</p>
  
  <pre><code>package com.example.myfirstandroidapp;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.widget.EditText;
  
  public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    }
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
    }
    /** 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.txtName);
      String message = editText.getText().toString();
      intent.putExtra(EXTRA_MESSAGE, message);
      startActivity(intent);
    }
  }
  </code></pre>
  
  <p>DisplayActivity.java,</p>
  
  <pre><code>package com.example.myfirstandroidapp;
  
  import android.annotation.TargetApi;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Build;
  import android.os.Bundle;
  import android.support.v4.app.NavUtils;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.TextView;
  
  public class DisplayMessageActivity extends Activity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // 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);
  
      // Show the Up button in the action bar.
      setupActionBar();
    }
  
    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
      }
    }
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.display_message, menu);
      return true;
    }
  
    @Override
    public void onDestroy(){
      super.onDestroy();
    }
  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
      }
      return super.onOptionsItemSelected(item);
    }
  
  
  }
  </code></pre>
  
  <p>activity_main.xml,</p>
  
  <pre><code>    &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context=".MainActivity" &gt;
  
          &lt;EditText
              android:id="@+id/txtName"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="70dp"
              android:ems="10" &gt;
  
              &lt;requestFocus /&gt;
          &lt;/EditText&gt;
  
          &lt;Button
              android:id="@+id/btnGo"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBottom="@+id/txtName"
              android:layout_alignParentRight="true"
              android:onClick="sendMessage"
              android:text="Go!" /&gt;
  
          &lt;TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/txtName"
              android:layout_alignParentTop="true"
              android:layout_marginTop="18dp"
              android:text="Please input your name:"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
      &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>activity_display_message.xml,   </p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".DisplayMessageActivity" &gt;
  
      &lt;TextView
          android:id="@+id/textView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" /&gt;
  
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>AndroidManifest.xml,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myfirstandroidapp"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="9"
          android:targetSdkVersion="10" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.myfirstandroidapp.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name="com.example.myfirstandroidapp.DisplayMessageActivity"
              android:label="@string/title_activity_display_message"
              android:parentActivityName="com.example.myfirstandroidapp.MainActivity" &gt;
              &lt;meta-data
                  android:name="android.support.PARENT_ACTIVITY"
                  android:value="com.example.myfirstandroidapp.MainActivity" /&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>