In my MainActivity
I call
MyDialog dialog = new MyDialog(MainActivity.this);
dialog.show();
MyDialog
is my own class where I customize the dialog.
In the dialog is a button. I want that the MainActivity
and the dialog finishes/dissappears when the button is pressed, because I start another Activity then.
How can I say in the MyDialog
class, in the onClickListener
, that the MainActivity
should finish()
?
Shortened code of my dialog:
public class MyDialog extends Dialog implements OnClickListener {
void onClick() {
Intent menu = new Intent(getContext(), Menu.class);
getContext().startActivity(menu);
}
}
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
When the user is on Activity D and click a button called exit, the application should go back to Activity B and finish the Activities C and D.
AlertDialog dialog = (AlertDialog) getDialog(); dialog. getButton(AlertDialog. BUTTON_POSITIVE). setEnabled(false);
You can use the methods cancel() or dismiss() . The method cancel() essentially the same as calling dismiss(), but it will also call your DialogInterface.
You can finish your Activity as below...
Intent intent = new Intent(context, YourSecondActivity.class);
context.startActivity(intent);
((Activity) context).finish();
Update:
In your constructor of you custom dialog class, get the activity context as below...
Context mContext;
public myDialog(Context context) {
super(context);
this.mContext = context;
}
then in your onClick()
method finish the activity as below...
@Override
public void onClick(View v) {
Intent menu = new Intent(mContext, menu.class);
mContext.startActivity(menu);
((Activity) mContext).finish();
}
Firstly in your dialog class pass the context of the caller activities say MainActivit.class context
Now first close the dialog
//so as to avoid the window leaks as on destroying the activity it's context would also get vanished.
dialog.dismiss();
and then
((Activity) context).finish();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With