title: How to work with onBackPressed() in Android?
body: <p>I have Activity 'A', Activity 'B', Activity 'C', Activity 'D', Activity 'E', Activity 'E' . I want to exit the Activity click on onBackPressed(). When i run the app it goes to  Activity 'A' it does not exit the app. How can i implement with this. </p>
  
  <p>Here is my Code</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      TableLayout table_layout;
      EditText firstname_et, lastname_et;
      Button addmem_btn;
  
      DatabaseHelper databaseHelper = new DatabaseHelper(this);
      protected SQLiteDatabase db;
  
      ProgressDialog PD;
      ImageView imgButtonBack;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.performance_report);
  
          table_layout = (TableLayout) findViewById(R.id.tableLayout_PerformanceReport);
  
  
           //ImageView imgButtonBack;
           imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);
           imgButtonBack.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  Intent i = new Intent(MainActivity.this, Employee_List.class);
                  i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(i);
                  //finish();
              }
          });
  
  
      }
  
  }
      @Override
      public void onBackPressed()
      {
          AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);
  
          alertbox.setTitle("Do you wish to exit ?");
          alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface arg0, int arg1) { 
                 // finish used for destroyed activity
                  finish();
              }
          });
  
          alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface arg0, int arg1) {
                      // Nothing will be happened when clicked on no button 
                      // of Dialog     
            }
          });
  
          alertbox.show();
     }
  }
  </code></pre>
  
  <p>Thanks to Appreciate.</p>