在对话框类中完成活动 [英] Finish activity in dialog class
问题描述
在我的 MainActivity
我调用
myDialog dialog = new myDialog(MainActivity.this);
dialog.show();
myDialog
是我自己的类,我自定义对话框。
在对话框中有一个按钮。我希望按钮按下 MainActivity
,并且对话框完成/消失,因为我开始另一个Activity。
如何在 myDialog
类中,在 onClickListener
中,说 MainActivity
应该 finish()
?
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()
?
推荐答案
p>您可以完成以下活动...
You can finish your Activity as below...
Intent intent = new Intent(context, YourSecondActivity.class);
context.startActivity(intent);
((Activity) context).finish();
更新:
在你的自定义对话框类的构造函数中,获取如下的活动上下文...
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;
}
然后在您的 onClick()
方法完成以下活动...
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();
}
这篇关于在对话框类中完成活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!