title: How to finish an Activity inside a dialog? I called finish(); and SecondActivity.this.finish(); but they didn't work
body: <p>In the first case, finish() works properly, but it doesn't work inside the dialog.
  This code is from second Activity, in the first case when I call finish() it finishes and return back to MainActivity but in the second case inside the dialog when I call <code>finish();</code> the app crashes.</p>
  <p>I can do something like</p>
  <p><code> startActivity(new Intent(getBaseContext(), MainActivity.class));</code></p>
  <p>inside the dialog, but I don't want to recreate the activity. I want to call finish(); inside the dialog.</p>
  <pre><code> @Override
  public boolean onOptionsItemSelected(@NonNull MenuItem item) {
      Intent intent = new Intent();
      String content = etContentDetail.getText().toString();
      int id = getIntent().getIntExtra(&quot;id&quot;,0);
      DbHelper dbHelper = new DbHelper(this);
  
      switch (item.getItemId()) {
  
          case R.id.update:
  
              dbHelper.noteUpdate(id, content, DateTime.date(), DateTime.time(), System.currentTimeMillis());
              setResult(RESULT_OK, intent);
            finish()
            break;
  
          case R.id.delete:
  
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              builder.setTitle(&quot;Delete Note&quot;);
              builder.setMessage(&quot;Are you sure you want to delete this note?&quot;);
              builder.setPositiveButton(&quot;Continue &quot;, new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i) {
                      dbHelper.deleteNote(id);
                      Toast.makeText(getApplicationContext(), &quot;Note Deleted&quot;, Toast.LENGTH_SHORT).show();
                      setResult(RESULT_OK, intent);
                      finish();
                  }
              });
              builder.setNegativeButton(&quot;Cancel &quot;, new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i) {
  
                  }
              }).create().show();
  
              break;
      }
          return true;
      }
  </code></pre>