gpt4 book ai didi

android - ProgressDialog 的 OnCancelListener 上的完成 Activity 不起作用

转载 作者:行者123 更新时间:2023-11-29 22:12:04 26 4
gpt4 key购买 nike

在我的 Activity 中,我从 Web 服务加载一些数据,在此期间我向用户显示 ProgressDialog ...我希望用户能够在按下 BACK D-Pad 键时关闭 Activity 。我是这样做的:

public class MyActivity implements OnCancelListener {
/* ... */

public void startDialog() {
ProgressDialog pd = ProgressDialog.show(
MyActivity.this, // Context
"", // title for dialog
"Loading...", // message for dialog
true, // indeterminate?
true, // cancellable?
this // onCancelListener()
);
}

@Override
public void onCancel(DialogInterface dialog) {
// I want to finish() this activity when dialog is canceled
finish();
}
}

您可以看到我正在 Dialog 的 OnCancelListener 接口(interface)的 onCancel() 方法中完成当前 Activity 。但是当我在对话框可见期间按 BACK 时,不会调用此方法。对话框消失,但未调用 onCancel。

是吗?

最佳答案

可能是安卓限制吧。使用处理程序试试

public class MyActivity implements OnCancelListener {
/* ... */

private Handler mHandler = new Handler(){

@Owerride
public void handleMessage(Message message) {
switch (message.what) {
case 1:{
finish();
} break;
}
}
};

public void startDialog() {
ProgressDialog pd = ProgressDialog.show(
MyActivity.this, // Context
"", // title for dialog
"Loading...", // message for dialog
true, // indeterminate?
true, // cancellable?
this // onCancelListener()
);
}

@Override
public void onCancel(DialogInterface dialog) {
mHandler.sendEmptyMessage(1);
}
}

关于android - ProgressDialog 的 OnCancelListener 上的完成 Activity 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9382038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com