Reputation: 2425
Everytime i try to show a alert dialog to the user and the user dismisses the dialog using the button inside the alert. I get the following error.
E/WindowManager: android.view.WindowLeaked: Activity SplashActivity has leaked window DecorView@5f814ed[SplashActivity] that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:765)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:440)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
at android.app.Dialog.show(Dialog.java:473)
at SplashActivity.errorDialogue(SplashActivity.java:363)
at SplashActivity$2.run(SplashActivity.java:196)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
The code I have written and tried is below:
private AlertDialog errorApiAlertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (getIntent().getStringExtra(getString(R.string.POS_CODE)) != null) {
RestApi restApi = new RestApi(this, context);
restApi.checkPOS(getIntent().getStringExtra(getString(R.string.RTR_TOKEN_ID)), getIntent().getStringExtra(getString(R.string.DEVICE_IMEI)),
getIntent().getStringExtra(getString(R.string.TRANSACTION_ID)));
System.out.println("token in skitto: " + getIntent().getStringExtra(getString(R.string.RTR_TOKEN_ID)));
System.out.println("imei in skitto: " + getIntent().getStringExtra(getString(R.string.DEVICE_IMEI)));;
System.out.println("transaction in skitto: " + getIntent().getStringExtra(getString(R.string.TRANSACTION_ID)));
System.out.println("POS in skitto: " + getIntent().getStringExtra(getString(R.string.POS_CODE)));
}
}
@Override
protected void onResume() {
super.onResume();
if (getIntent().getStringExtra(getString(R.string.POS_CODE)) != null) {
RestApi restApi = new RestApi(this, context);
restApi.checkPOS(getIntent().getStringExtra(getString(R.string.RTR_TOKEN_ID)), getIntent().getStringExtra(getString(R.string.DEVICE_IMEI)),
getIntent().getStringExtra(getString(R.string.TRANSACTION_ID)));
System.out.println("token in skitto: " + getIntent().getStringExtra(getString(R.string.RTR_TOKEN_ID)));
System.out.println("imei in skitto: " + getIntent().getStringExtra(getString(R.string.DEVICE_IMEI)));;
System.out.println("transaction in skitto: " + getIntent().getStringExtra(getString(R.string.TRANSACTION_ID)));
System.out.println("POS in skitto: " + getIntent().getStringExtra(getString(R.string.POS_CODE)));
}
}
Then inside the onResponseLoaded function I used the runnable funcation to display the dialog as below:
runOnUiThread(new Runnable() {
@Override
public void run() {
errorDialogue("Incorrect validation. Please try again"); <-- error here
}
});
public void errorDialogue(String message) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_failed_pos_login, null);
dialogBuilder.setView(dialogView);
TextView descriptionTextView = (TextView) dialogView.findViewById(R.id.failedMsg);
descriptionTextView.setText(message);
//Do your UI operations like dialog opening or Toast here
errorApiAlertDialog = dialogBuilder.create();
errorApiAlertDialog.show(); <-- error here
final Button retryBtn = (Button) dialogView.findViewById(R.id.btntryAgain);
retryBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
errorApiAlertDialog.dismiss();
finish();
}
});
}
After reading many other stackoverflow questions on this topic, I added onPause() and onStop() methods as following:
@Override
protected void onPause() {
super.onPause();
if (errorApiAlertDialog != null && errorApiAlertDialog.isShowing()) {
errorApiAlertDialog.dismiss();
}
}
@Override
protected void onStop() {
super.onStop();
if (errorApiAlertDialog != null && errorApiAlertDialog.isShowing()) {
errorApiAlertDialog.dismiss();
}
}
My application is not crashing though. The application shows the alert dialog and when i press the try again button the app closes and the above error is shown. Tried many other possible ways to solve this issue. What am i doing wrong here that the activity has leaked window?
Upvotes: 1
Views: 2600
Reputation: 2425
Fixed the error after removing the following duplicate code from onCreate()
if (getIntent().getStringExtra(getString(R.string.POS_CODE)) != null) {
RestApi restApi = new RestApi(this, context);
.......//rest code
}
As the code was same in both onResume and onCreate, I was getting Activity has leaked windows. Now after removing the code from onCreate everything is fine.
Upvotes: 2