Finish activity in dialog class

后端 未结 2 1635
没有蜡笔的小新
没有蜡笔的小新 2020-12-17 16:56

In my MainActivity I call

 myDialog dialog = new myDialog(MainActivity.this);
 dialog.show();

myDialog is my own

相关标签:
2条回答
  • 2020-12-17 17:41

    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();
    }
    
    0 讨论(0)
  • 2020-12-17 17:48

    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();
    
    0 讨论(0)
提交回复
热议问题