Error WindowLeaked when displaying a Dialog

1

Performing some tests to display a custom dialog box of class Dialog , you are throwing me an error of type " android.view.WindowLeaked " when you rotate the screen when it is displayed. From what I have read on the Internet it is an error because I show the dialog box when the activity no longer exists, but I do not know how to solve it.

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Dialog oDialog = new Dialog(this);
        oDialog.setContentView(R.layout.layout_error);
        oDialog.setCancelable(false);
        oDialog.setCanceledOnTouchOutside(false);
        oDialog.show();
    }
}

I add a trace of the error:

E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb9af0c50
E/WindowManager: android.view.WindowLeaked: Activity app.test.myapplication.MainActivity has leaked window com.android.internal.policy.PhoneWindow$DecorView{ca325cd V.E...... R....... 0,0-180,356} that was originally added here
                 at android.view.ViewRootImpl.<init>(ViewRootImpl.java:375)
                 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299)
                 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                 at android.app.Dialog.show(Dialog.java:319)
                 at app.test.myapplication.MainActivity.onCreate(MainActivity.java:19)
                 at android.app.Activity.performCreate(Activity.java:6245)
                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                 at android.os.Handler.dispatchMessage(Handler.java:102)
                 at android.os.Looper.loop(Looper.java:148)
                 at android.app.ActivityThread.main(ActivityThread.java:5443)
                 at java.lang.reflect.Method.invoke(Native Method)
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)'
    
asked by Oemtri 01.03.2018 в 12:04
source

2 answers

2

The error:

  

MainActivity has leaked window ... that was originally added here

Indicates that by rotating the activity generated by the Dialogue, it is destroyed and the Dialogue does not find the Activity on which the dialogue was originally created.

You can define your Activity within your AndroidManifest.xml that does not destroy the Activity when rotating:

<activity
    ...
    android:configChanges="keyboardHidden|orientation|screenSize"
    .../>
        ...
</activity>

Another option is to determine when the Activity is going to be destroyed and avoid creating the Dialog:

 static boolean isAvailable = false;

  @Override
  public void onStart() {
     super.onStart();

     isAvailable = true;

  } 

  @Override
  public void onStop() {
     super.onStop();

     isAvailable = false;

  }

So that in the points where you want to create the Dialog, it is not shown if the Activity is about to be destroyed:

 if(isAvailable){ //Actividad Activa
    Dialog oDialog = new Dialog(this);
    oDialog.setContentView(R.layout.layout_error);
    oDialog.setCancelable(false);
    oDialog.setCanceledOnTouchOutside(false);
    oDialog.show();
  }
    
answered by 01.03.2018 / 14:52
source
0

Test first by inflating the dialog view before passing it

val view = LayoutInflater.from(this).inflate(R.layout.layout_error, null)
Dialog oDialog = new Dialog(this);
oDialog.setContentView(view);
oDialog.setCancelable(false);
oDialog.show();
    
answered by 01.03.2018 в 15:38