我使用CustomDialog
为BackBTN创建CustomDialogClass
,如下所示:
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时,此对话框都存在。所以我想从像cdd.dismise;
这样的MainActivity类关闭/推销它。但不幸的是,它不会发生。你能帮帮我吗?感谢
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;
}
}
}
我想在cdd
中关闭/推销NOTE_RESTORE
。
答案 0 :(得分:1)
如果要在离开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;
}
}
答案 1 :(得分:0)
如果您要从活动中删除对话框
dialog.dismiss();
如果要从该对话框类中删除对话框而不使用
this.dismiss();