View not attached to window manager crash
How to reproduce the bug:
- Enable this option on your device:
Settings -> Developer Options -> Don't keep Activities
. - Press Home button while the
AsyncTask
is executing and theProgressDialog
is showing.
The Android OS will destroy an activity as soon as it is hidden. When onPostExecute
is called the Activity
will be in "finishing" state and the ProgressDialog
will be not attached to Activity
.
How to fix it:
- Check for the activity state in your
onPostExecute
method. - Dismiss the
ProgressDialog
inonDestroy
method. Otherwise,android.view.WindowLeaked
exception will be thrown. This exception usually comes from dialogs that are still active when the activity is finishing.
Try this fixed code:
public class YourActivity extends Activity {
private void showProgressDialog() {
if (pDialog == null) {
pDialog = new ProgressDialog(StartActivity.this);
pDialog.setMessage("Loading. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
}
pDialog.show();
}
private void dismissProgressDialog() {
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
}
@Override
protected void onDestroy() {
dismissProgressDialog();
super.onDestroy();
}
class LoadAllProducts extends AsyncTask<String, String, String> {
// Before starting background thread Show Progress Dialog
@Override
protected void onPreExecute() {
showProgressDialog();
}
//getting All products from url
protected String doInBackground(String... args) {
doMoreStuff("internet");
return null;
}
// After completing background task Dismiss the progress dialog
protected void onPostExecute(String file_url) {
if (YourActivity.this.isDestroyed()) { // or call isFinishing() if min sdk version < 17
return;
}
dismissProgressDialog();
something(note);
}
}
}
android View not attached to window manager
I had this issue where on a screen orientation change, the activity finished before the AsyncTask with the progress dialog completed. I seemed to resolve this by setting the dialog to null onPause()
and then checking this in the AsyncTask before dismissing.
@Override
public void onPause() {
super.onPause();
if ((mDialog != null) && mDialog.isShowing())
mDialog.dismiss();
mDialog = null;
}
... in my AsyncTask:
protected void onPreExecute() {
mDialog = ProgressDialog.show(mContext, "", "Saving changes...",
true);
}
protected void onPostExecute(Object result) {
if ((mDialog != null) && mDialog.isShowing()) {
mDialog.dismiss();
}
}
View=DecorView@47b2334[Connecting] not attached to window manager
this error comes when you are trying to attach the progress dialog to an Activity that is already destroyed or in the background. Check if the activity is running by :
activity.isfinishing()
if it is false then attach the progressdialog
.
@Override
protected void onPreExecute() {
progressDialog.setTitle("Connecting");
progressDialog.setMessage("Trying to connect to your server. Please wait...");
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
if(!context.isFinishing()){
progressDialog.show();
}
}
View not attached to window manager, dialog dismiss
Use a try statement.
new CountDownTimer(700, 100) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
try {
dialog.dismiss();
dialog = null;
} catch (Exception e) {
//TODO: Fill in exception
}
}
}.start();
View not attached to window manager (whats the solution?)
Dismiss the dialog in your onFinish
or onDestroy
of the launching activity.
Or/And
Don't let your activity handle the orientation changes.
You might find some help in these answers :
java.lang.IllegalArgumentException: View not attached to window manager
Related Topics
Android: Crop an Image After Taking It with Camera with a Fixed Aspect Ratio
How to Know That My Webview Is Loaded 100%
How to Align Title at Center of Actionbar in Default Theme(Theme.Holo.Light)
Android Mask Bitmap on Canvas Gen a Black Space
No Good Example About Recyclerview and Staggeredgridlayoutmanager in Android Docs
How to Get App Market Version Information from Google Play Store
Android Error - Close() Was Never Explicitly Called on Database
How to Display Activity When the Screen Is Locked
Android.View.View.Systemuivisibility Deprecated. What Is the Replacement
Camera Tutorial for Android (Using Surfaceview)
Android Vector Drawable App:Srccompat Not Showing Images
How to Get the Current Location in Google Maps Android API V2
Converting Activity into Fragment
Intent - If Activity Is Running, Bring It to Front, Else Start a New One (From Notification)
Auto-Scrolling Textview in Android to Bring Text into View