Android activity leaked into the window com.android.internal.policy.impl.phonewindow $ decorview Issue

I am working with an Android application to show a network error.

NetErrorPage.java

package exp.app; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class NetErrorPage extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.neterrorlayout); Button reload=(Button)findViewById(R.id.btnReload); reload.setOnClickListener(this); showInfoMessageDialog("Please check your network connection","Network Alert"); } public void onClick(View arg0) { if(isNetworkAvailable()) { Intent myIntent = new Intent((Activity)NetErrorPage.this, MainActivity.class); myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); ((Activity)NetErrorPage.this).startActivity(myIntent); finish(); } else showInfoMessageDialog("Please check your network connection","Network Alert"); } public void showInfoMessageDialog(String message,String title) { AlertDialog alertDialog = new AlertDialog.Builder(NetErrorPage.this).create(); alertDialog.setTitle("Network Alert"); alertDialog.setMessage(message); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { dialog.cancel(); } }); alertDialog.show(); } private boolean isNetworkAvailable() { NetworkInfo ActiveNetwork; @SuppressWarnings("unused") String IsNetworkConnected; @SuppressWarnings("unused") String ConnectionType; ConnectivityManager connectivitymanager; connectivitymanager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); try { ActiveNetwork=connectivitymanager.getActiveNetworkInfo(); ConnectionType=ActiveNetwork.getTypeName(); IsNetworkConnected=String.valueOf(ActiveNetwork.getState()); return true; } catch(Exception error) { return false; } } } 

but I get an error as shown below

 08-17 11:59:08.019: E/WindowManager(16460): Activity exp.app.NetErrorPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534a18 that was originally added here 08-17 11:59:08.019: E/WindowManager(16460): android.view.WindowLeaked: Activity exp.app.NetErrorPage has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534a18 that was originally added here 08-17 11:59:08.019: E/WindowManager(16460): at android.view.ViewRoot.<init>(ViewRoot.java:263) 08-17 11:59:08.019: E/WindowManager(16460): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 08-17 11:59:08.019: E/WindowManager(16460): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 08-17 11:59:08.019: E/WindowManager(16460): at android.view.Window$LocalWindowManager.addView(Window.java:424) 08-17 11:59:08.019: E/WindowManager(16460): at android.app.Dialog.show(Dialog.java:241) 08-17 11:59:08.019: E/WindowManager(16460): at sync.directtrac.NetError.showInfoMessageDialog(NetErrorPage.java:114) 08-17 11:59:08.019: E/WindowManager(16460): at sync.directtrac.NetError.onCreate(NetErrorPage.java:26) 08-17 11:59:08.019: E/WindowManager(16460): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 08-17 11:59:08.019: E/WindowManager(16460): at android.os.Handler.dispatchMessage(Handler.java:99) 08-17 11:59:08.019: E/WindowManager(16460): at android.os.Looper.loop(Looper.java:130) 08-17 11:59:08.019: E/WindowManager(16460): at android.app.ActivityThread.main(ActivityThread.java:3687) 08-17 11:59:08.019: E/WindowManager(16460): at java.lang.reflect.Method.invokeNative(Native Method) 08-17 11:59:08.019: E/WindowManager(16460): at java.lang.reflect.Method.invoke(Method.java:507) 08-17 11:59:08.019: E/WindowManager(16460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 08-17 11:59:08.019: E/WindowManager(16460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 08-17 11:59:08.019: E/WindowManager(16460): at dalvik.system.NativeStart.main(Native Method) 

I searched more ... but I have no right idea to clear this.

I want a layout to be added when this page loads and a dialog is shown.

Please help me fix this error.

Note: I tried this as well

 @Override protected void onResume() { super.onResume(); runOnUiThread(new Runnable() { public void run() { showInfoMessageDialog("Please check your network connection","Network Alert"); } }); } 
+45
android android-layout memory-leaks android-alertdialog
Aug 17
source share
7 answers

Thanks guys for giving me a lot of suggestions . Finally, I got a solution. That is, I started NetErrorPage twice. Once, I checked the availability of the network connection and started the intent in the event started on the page. the second time, if the page has an error, then I started my intention in the OnReceivedError event. Therefore, the first dialog does not close, before this the second dialog box is called. So I got an error.

Cause of error : I called the showInfoMessageDialog method two times before closing the first.

Now I deleted the second call and dropped the error :-).

+60
Aug 17 2018-12-12T00:
source share

Change this dialog.cancel(); on dialog.dismiss();

The solution is to call dismiss() on the Dialog you created in NetErrorPage.java:114 before exiting the Activity , for example. in onPause() .

Views have a reference to their parent Context (taken from the constructor argument). If you leave the Activity without destroying Dialog and other dynamically created View s, they still save this link on your Activity (if you created it with Context : like new ProgressDialog(this) ), the GC cannot be collected, which will lead to a leak memory.

+44
Aug 17 2018-12-12T00:
source share

Please try this way and let me know:

 Context mContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.neterrorlayout); mContext=NetErrorPage.this; Button reload=(Button)findViewById(R.id.btnReload); reload.setOnClickListener(this); showInfoMessageDialog("Please check your network connection","Network Alert"); } public void showInfoMessageDialog(String message,String title) { new AlertDialog.Builder(mContext) .setTitle("Network Alert"); .setMessage(message); .setButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { dialog.cancel(); } }) .show(); } 
+4
Aug 17 2018-12-12T00:
source share

In my case, finish () is executed immediately after the dialog box is shown.

+4
Jun 20 '16 at 2:53 on
source share

The dialog should only start after the initialization of the states of the Activity window. This happens only after onresume.

So call

 runOnUIthread(new Runnable(){ showInfoMessageDialog("Please check your network connection","Network Alert"); }); 

in your OnResume function. Do not create dialogs in OnCreate
Edit:

use this

 Handler h = new Handler(); h.postDelayed(new Runnable(){ showInfoMessageDialog("Please check your network connection","Network Alert"); },500); 

in your Onresume instead of showonuithread

+3
Aug 17 '12 at 7:10
source share
  @Override protected void onPostExecute(final Boolean success) { mProgressDialog.dismiss(); mProgressDialog = null; 

setting null works for me

+1
Jul 01 '17 at 4:36 on
source share

I got this error, but it was interesting. As the first, I got this error at api level 17. When I call a thread (AsyncTask or others) without a progress dialog, I again call another thread method using the progress dialog, I got this failure, and the reason is to use the progress dialog ,

In my case, there are two results:

  • I took show(); the execution dialog method before the first start of the thread, after which I took dismiss(); progress dialogue method before the last thread ends.

So:

 ProgresDialog progressDialog = new ... //configure progressDialog progressDialog.show(); start firstThread { ... } ... start lastThread { ... } //be sure to finish threads progressDialog.dismiss(); 
  • It is so strange, but this error has the ability to destroy itself, at least for me :)
0
Dec 14 '16 at 15:22
source share



All Articles