如何从 android 中的 Activity close/dismise 对话 class?

how to close/dismise dialog class from Activity in android?

我使用 CustomDialogClass 为 BackBTN 创建了 CustomDialog,如下所示:

    public void onBackPressed () {
    CustomDialogClass cdd = new CustomDialogClass(Adult1Activity.this);
    cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    cdd.show();
}


public class CustomDialogClass extends Dialog {
    public Activity c;
    public Button minimizeBTN, exitBTN;

    public CustomDialogClass(Activity a) {
        super(a);
        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.back_btn);
        minimizeBTN = (Button) findViewById(R.id.minimizing_app);
        exitBTN = (Button) findViewById(R.id.exit_completely);

       ...

    }

    @Override
    public void dismiss() {
        super.dismiss();
        //cdd.dismiss();
    }
}

每当我回到 MainActivity 时,这个对话框就存在。所以我想 close/dismise 从 MainActivity class 像 cdd.dismise; 一样。但不幸的是,这并没有发生。你能帮帮我吗?谢谢

MainActivity.class:

 public class MainActivity extends AppCompatActivity{
    public CustomDialogClass cdd; // back dialog
    private boolean isRunning = false;
    public String timeString;
    public NotificationManager notificationManager;
    private static final String EXTRA_NOTE = "NOTE";
    private static final String NOTE_RESTORE = "restore";
    private static final String NOTE_CLOSE = "close";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adult1);

        handleIntent(getIntent());
    }

    //alert dialog for back btn
    public void onBackPressed () {
        CustomDialogClass cdd = new CustomDialogClass(Adult1Activity.this);
        cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        cdd.show();
    }


    public class CustomDialogClass extends Dialog {
        public Activity c;
        public Button minimizeBTN, exitBTN;

        public CustomDialogClass(Activity a) {
            super(a);
            this.c = a;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.back_btn);
            minimizeBTN = (Button) findViewById(R.id.minimizing_app);
            exitBTN = (Button) findViewById(R.id.exit_completely);

            minimizeBTN.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MinimizeApp();
                    isRunning = true;

                    ExtendedNotification(timeString);
                }
            });


            exitBTN.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CloseApp();
                }
            });

        }

        @Override
        public void dismiss() {
            super.dismiss();
        }
    }

//*****************Notification class******
    private void ExtendedNotification(String time) {
        final Intent resultIntentRestore = new Intent(this, Adult1Activity.class);
        resultIntentRestore.putExtra(EXTRA_NOTE, NOTE_RESTORE);
        PendingIntent restoreIntent = PendingIntent.getActivity(Adult1Activity.this,
                0, resultIntentRestore, PendingIntent.FLAG_UPDATE_CURRENT);

        final Intent resultIntentClose = new Intent(this, Adult1Activity.class);
        resultIntentClose.putExtra(EXTRA_NOTE, NOTE_CLOSE);
        PendingIntent closeIntent = PendingIntent.getActivity(Adult1Activity.this,
                2, resultIntentClose, PendingIntent.FLAG_UPDATE_CURRENT);

         final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Adult1Activity.this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("App Name")
                .setContentText(time)
                .setAutoCancel(true)
                .addAction(new NotificationCompat.Action(R.mipmap.ic_launcher, "Restore", restoreIntent))
                .addAction(new NotificationCompat.Action(R.mipmap.ic_launcher, "Close", closeIntent))
                .setContentIntent(restoreIntent);

         notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = notificationBuilder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

        notificationManager.notify(0, notification);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent); // Make sure to call super
        handleIntent(intent);
}

    private void handleIntent(Intent intent) {
        final String a = intent.getStringExtra(EXTRA_NOTE);
        if (a != null) {
            switch (a) {
                case NOTE_RESTORE:
                    tv.setText(timeString);
                    **cdd.dismiss();**

                    break;

                case NOTE_CLOSE:
                    countDownTimer.cancel();
                    isRunning = false;
                    notificationManager.cancel(0);
                    CloseApp();
                    break;
            }
        }
    }

我想 close/dismise cddNOTE_RESTORE.

如果您想在每次离开时隐藏对话框 Activity,请将对话框保留为成员,并在 onStop()

时将其关闭
// Keep track of the dialog
private CustomDialogClass cdd;

//alert dialog for back btn
public void onBackPressed () {
    // Assign to a member instead, not the local variable
    cdd = new CustomDialogClass(Adult1Activity.this);
    cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    cdd.show();
}

@Override
protected void onStop() {
    super.onStop();
    if (cdd != null) {
        cdd.dismiss();
        cdd = null;
    }
}

如果要从 activity

中删除对话框
dialog.dismiss();

如果您想从该对话框中删除对话框 class 而不是使用

this.dismiss();