ProgressDialog deviates after screen rotation

I use the Service to upload files. While the file is loading, I show the ProgressDialog.

When I rotate the screen, the ProgressDialog is rejected.

How can I save a ProgressDialog?

I would prefer not to override onConfigurationChanged.

Thanks!

+4
source share
2 answers

Android should handle this automatically if you show your dialog using the showDialog(int) method.

+3
source

Expanding at the suggestion of Dalmas, if you turned out to be like me, where you created a dialog in a separate class to control the async task, then one line might be all you need to add:

 mProgressDialog.setOwnerActivity((Activity)mContext); 

This allows the Office to manage the dialogue. Longer snippet for reference ...

 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(); } 
+1
source

Source: https://habr.com/ru/post/1382000/


All Articles