我使用服务来上传文件,在文件正在上传时,我会显示一个 ProgressDialog(进度对话框)。
当我旋转屏幕时,ProgressDialog(进度对话框)会消失。
我该如何保持 ProgressDialog (进度对话框)的存在?
我不想覆盖 onConfigurationChanged 方法。
谢谢!
我使用服务来上传文件,在文件正在上传时,我会显示一个 ProgressDialog(进度对话框)。
当我旋转屏幕时,ProgressDialog(进度对话框)会消失。
我该如何保持 ProgressDialog (进度对话框)的存在?
我不想覆盖 onConfigurationChanged 方法。
谢谢!
如果您使用showDialog(int)
方法显示对话框,Android应该会自动处理此操作。
在Dalmas的建议基础上,如果你和我一样,在单独的类中创建了对话框来管理异步任务,那么可能只需要添加一行代码:
mProgressDialog.setOwnerActivity((Activity)mContext);
这使得Activity能够管理对话框。以下是更长的片段供参考...
public DownloadCatalog(Context c) {
mContext = c;
}
public void startDownload() {
mProgressDialog = new ProgressDialog(mContext);
mProgressDialog.setOwnerActivity((Activity)mContext);
mProgressDialog.setMessage("Downloading \n" + remoteDirectory);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
new DownloadFileAsync().execute(fileUrl);
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}