title: How to navigate to previous activity when back icon is clicked instead of the complete toolbar in Android?
body: <p>I am developing an android application and want to navigate up to the previous activity from the current activity.</p>
  
  <p>Here, I want to navigate back to MainActivity from SecondActivity. I have added a toolbar and a back icon, which should navigate back to the previous activity when clicked. But here, the complete toolbar navigates back to the previous activity when clicked.</p>
  
  <p>SecondActivity.java</p>
  
  <pre><code>package com.example.acer.videoapp;
  
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.Toolbar;
  import android.util.Log;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.SimpleAdapter;
  import android.widget.Toast;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  import java.util.ArrayList;
  import java.util.HashMap;
  
  
  public class SecondActivity extends AppCompatActivity {
  
      private String TAG = MainActivity.class.getSimpleName();
      private ProgressDialog pDialog;
      private ListView listView1;
      Toolbar toolbar1;
      String subjectName;
  
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; lessonList;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_second);
  
          //setting title to toolbar
          toolbar1 = (Toolbar) findViewById(R.id.toolbar1);
  
          Bundle bundle = getIntent().getExtras();
          if(bundle!=null) {
              toolbar1.setTitle(bundle.getString("Subject"));
              subjectName=toolbar1.getTitle().toString();
          }
  
          lessonList = new ArrayList&lt;&gt;();
          listView1 = (ListView) findViewById(R.id.list);
          new GetLessons().execute();
  
          listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int i, long id) {
                  Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                  HashMap&lt;String, String&gt; lesson = lessonList.get(i);
                  intent.putExtra("number", lesson.get("number"));
                  intent.putExtra("name", lesson.get("name"));
                  //intent.putExtra("subject", listView1.getItemAtPosition(i).toString());
                  startActivity(intent);
              }
          });
  
          //back navigation
          toolbar1.setNavigationIcon(R.drawable.back);
          toolbar1.setNavigationOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  startActivity(new Intent(getApplicationContext(),MainActivity.class));
              }
          });
  
      }
  
      /* Async task class to get json by making HTTP call */
      private class GetLessons extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              // Showing progress dialog
              pDialog = new ProgressDialog(SecondActivity.this);
              pDialog.setMessage("Please wait...");
              pDialog.setCancelable(false);
              pDialog.show();
  
          }
  
          @Override
          protected Void doInBackground(Void... arg0) {
              HttpHandler sh = new HttpHandler();
  
              // Making a request to url and getting response
              String url = getResources().getString(R.string.lessons_url, subjectName);
              String jsonStr = sh.makeServiceCall(url);
  
              Log.e(TAG, "Response from url: " + jsonStr);
  
              if (jsonStr != null) {
                  try {
                      // Getting JSON Array
                      JSONArray lessons = new JSONArray(jsonStr);
  
                      // looping through All lessons
                      for (int i = 0; i &lt; lessons.length(); i++) {
                          JSONObject c = lessons.getJSONObject(i);
  
                          String number = c.getString("lessonNo");
                          String name = c.getString("lessonName");
  
                          // tmp hash map for single lesson
                          HashMap&lt;String, String&gt; lesson = new HashMap&lt;&gt;();
  
                          // adding each child node to HashMap key =&gt; value
                          lesson.put("number", number);
                          lesson.put("name", name);
  
  
                          // adding lesson to lesson list
                          lessonList.add(lesson);
                      }
                  } catch (final JSONException e) {
                      Log.e(TAG, "Json parsing error: " + e.getMessage());
                      runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                              Toast.makeText(getApplicationContext(),"Json parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                          }
                      });
  
                  }
              } else {
                  Log.e(TAG, "Couldn't get json from server.");
                  runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          Toast.makeText(getApplicationContext(),"Couldn't get json from server. Check LogCat for possible errors!",Toast.LENGTH_LONG).show();
                      }
                  });
  
              }
  
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              super.onPostExecute(result);
              // Dismiss the progress dialog
              if (pDialog.isShowing())
                  pDialog.dismiss();
              /**
               * Updating parsed JSON data into ListView
               * */
              ListAdapter adapter = new SimpleAdapter(
                      SecondActivity.this, lessonList,R.layout.list_item, new String[]{"number", "name",}, new int[]{R.id.lnumber,R.id.lname});
              listView1.setAdapter(adapter);
  
  
  
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>activity_second.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"
      tools:context="com.example.acer.videoapp.SecondActivity"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:background="@color/blue"
          android.theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          /&gt;
  
      &lt;ListView
          android:id="@+id/list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:layout_below="@+id/toolbar1"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>