簡體   English   中英

如何從Android的Activity中關閉/分離對話框類?

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

我通過使用CustomDialogClass for 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時,都會存在此對話框。 所以我想從MainActivity類(如cdd.dismise;關閉/分離它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;
            }
        }
    }

我想在NOTE_RESTORE關閉/分離cdd

如果要在每次離開“活動”時都隱藏對話框,請將該對話框保留為成員,然后在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;
    }
}

如果要從活動中刪除對話框

dialog.dismiss();

如果要從該對話框類中刪除對話框而不是使用

this.dismiss();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM