Skip to content
Advertisement

How to finish an Activity inside a dialog? I called finish(); and SecondActivity.this.finish(); but they didn’t work

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 finish(); the app crashes.

I can do something like

startActivity(new Intent(getBaseContext(), MainActivity.class));

inside the dialog, but I don’t want to recreate the activity. I want to call finish(); inside the dialog.

JavaScript

Advertisement

Answer

Don’t directly call the methods you put inside your alert dialog button’s onClick. Move them into a single function inside DetailActivity.java instead and then call that function from the alert dialog.

In DetailActivity.java

JavaScript

In your AlertDialog

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement