The most common issue which leads your android application to crash with this log message:
Error: E/WindowManager: android.view.WindowLeaked: Activity com.example.android has leaked window
com.android.internal.policy.PhoneWindow$DecorView{c75fa80 V.E……
Let’s take an example here is request() method where we have requested to the server and showing a progress bar. Here mProgress is an instance of ProgressDialog and getResponse() is a method in which we get a response from the server if error(0) that mean we can start new activity for the user.
1 2 3 4 5 6 7 8 9 10 11 |
private void request(String URL,Context mContext){ mProgress=new Progress(mContext); mProgress.show(); new VolleyRequest(URL); } private void getResponse(JsonObject response){ if(response.getInt("error")==0){ startActivity(new Intent(this,OtherActivity.class)); } } |
In above code, we are showing the progress bar to the user and started the new activity without dismissing progress which leaked window in this case.
Prevention:
onStop() method Activity cycle called when no longer activity visible there we can check either mProgress is null or not. If mProgress isn’t null that mean your activity leaked window so you can dismiss it onStop() method.
Example:
1 2 3 4 5 6 7 |
@Override protected void onStop() { super.onStop(); if(mProgress!=null){ mProgress.dismiss; } } |
If you have any doubt comment below.