Activity Has Leaked Window That Was Originally Added

Activity has leaked window that was originally added

You're trying to show a Dialog after you've exited an Activity.

[EDIT]

This question is one of the top search on google for android developer,
therefore Adding few important points from comments, which might be more helpful for future investigator without going in depth of comment conversation.

Answer 1 :

You're trying to show a Dialog after you've exited an Activity.

Answer 2

This error can be a little misleading in some circumstances (although
the answer is still completely accurate) - i.e. in my case an
unhandled Exception was thrown in an AsyncTask, which caused the
Activity to shutdown, then an open progressdialog caused this
Exception.. so the 'real' exception was a little earlier in the log

Answer 3

Call dismiss() on the Dialog instance you created before exiting your
Activity, e.g. in onPause() or onDestroy()

Activity has leaked window that was originally added

You're trying to show a Dialog after you've exited an Activity.

[EDIT]

This question is one of the top search on google for android developer,
therefore Adding few important points from comments, which might be more helpful for future investigator without going in depth of comment conversation.

Answer 1 :

You're trying to show a Dialog after you've exited an Activity.

Answer 2

This error can be a little misleading in some circumstances (although
the answer is still completely accurate) - i.e. in my case an
unhandled Exception was thrown in an AsyncTask, which caused the
Activity to shutdown, then an open progressdialog caused this
Exception.. so the 'real' exception was a little earlier in the log

Answer 3

Call dismiss() on the Dialog instance you created before exiting your
Activity, e.g. in onPause() or onDestroy()

“Activity has leaked window that was originally added here”

public void syncSQLiteMySQLDB() {
// Create AsycHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
// Http Request Params Object
RequestParams params = new RequestParams();
// Show ProgressBar
prgDialog = new ProgressDialog(MainActivity.this);
prgDialog.setMessage("Transferring Data from Remote MySQL DB and Syncing SQLite. Please wait...");
prgDialog.setCancelable(false);
prgDialog.show();

// Make Http call to getusers.php
client.post("http://download.grupsapte.ro/sinc/getusers.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {

// Update SQLite DB with response sent by getusers.php
updateSQLite(response);
}
// When error occured
@Override
public void onFailure(int statusCode, Throwable error, String content) {
// TODO Auto-generated method stub

if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
Toast.LENGTH_LONG).show();
}
}

@Override
public void onFinish(){
// Hide ProgressBar
if(prgDialog.isShowing())
prgDialog.hide();
}
});

}

First of all you should declare local progress bar object and initialize with MainActivity.this also AsyncHttpClient have a method onFinish() which will call after success or failure called so you only need to hide your progress bar in onfinsh method and you always should check is progress bar is showing or not so it will not lead to generate excepetion

Activity has leaked window that was originally added here

You call Assignment_Create.this.finish(); directly after calling dialog.show(). This means that the dialog is on screen when your activity gets destroyed, which causes the leaked window exception. You need to close all dialogs by calling dialog.dismiss() before calling this.finish().

You have two options:

  1. Move this.finish() to inside your onClickListener for the
    alert dialog. This is what I think you want, so that the activity
    will finish only if the user clicks ok in the dialog.
  2. Call alertdialog.finish() directly before calling this.finish().

Activity has leaked window at alertDialog show() method

You get error because ProgressDialog is running while your Activity is destroyed. You should dismiss dialog before starting new Activity.

.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (alertDialog != null && alertDialog.isShowing()) {
alertDialog.dismiss();
}
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
}
});

I hope it helps!

Activity has leaked window that was originally added here (when login datas are wrong)

Call dismiss on Dialog:

    } else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login informations are wrong. Please check email and password")
.setNegativeButton("Retry",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//retry or dialog.dismiss();
}
})
.create()
.show();

}
} catch (JSONException e) {
e.printStackTrace();
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Webspace is currently not reachable.")
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
})
.create()
.show();
}

Android 9.0 Activity has leaked window that was originally added

Please check below scenario :

onStop() method Activity cycle called when no longer activity visible there we can check either mDialog is null or not. If mDialog isn’t null that mean your activity leaked window so you can dismiss it onStop() method.

@Override
protected void onStop() {
super.onStop();
if(mDialog!=null){
mDialog.dismiss;
}
}

Hope this will help!



Related Topics



Leave a reply



Submit