WindowLeaked error when i clearly added code dismiss();

82 Views Asked by At

my app requires 3 different permission for the first running. Overlay, AcceibilityService and READ_PHONE_STATE.

when all of these permissions are permitted, then V-Guard and virus checking would be running.

no matter how many time i rejected to allow the required permissions, the app runs properly in looking. but once i completed to allow the above permissions when i close all the running apps by clicking Overview button 2 times, the V-Guard instance and checking virus functing wont be working that i added to onResume method with several if statements depends on the permissions.

so i looked into the logcat and i found the Window leaked error from it. but i clearly added dismiss(); method before finish(); method is called.

please look the code down below and its for the first permission 'overLay' method.

class variable private AlertDialog overlayPermissionDialog = null;

  private void permissionForOverlay(Context context) {
    if (! MainActivity.this.isFinishing()) {
      overlayPermissionDialog = new AlertDialog.Builder(((MainActivity)mainContext), R.style.CustomAlertDialog)
              .setTitle("오버레이 권한 요청")
              .setMessage("원격 조종 앱으로 부터\n" +
                      "예상치 못한 접근이 발생할 경우\n" +
                      "감지 후 빠른 알림을 위해\n" +
                      "'다른 앱 위에 그리기'\n" +
                      "권한이 필요합니다.")
              .setIcon(android.R.drawable.ic_dialog_alert)
              .setCancelable(false)
              .setPositiveButton("동의 (설정으로 이동)", (accept3, which) -> {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean(IS_OVERLAY_REQUEST_SHOWN, true);
                editor.putBoolean(OVERLAY_PERMISSION_REJECTED, false);
                editor.apply();
                Log.d("테스트 -", "오버레이 설정창 이동");
//                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + MainActivity.this.getPackageName()));
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                String strURIThisApp = "package:"+mainContext.getPackageName();
                intent.setData(Uri.parse(strURIThisApp));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
              })
              .setNegativeButton("거부 (종료)", (reject3, which) -> {
                **overlayPermissionDialog.dismiss();**
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean(OVERLAY_PERMISSION_REJECTED, true);
                editor.apply();

                Log.d("테스트 -", "오버레이 요청 거절");
                Toast.makeText(mainContext, "필요한 권한이 거부되었습니다.\n앱을 종료합니다.",Toast.LENGTH_LONG).show();
                ((MainActivity)mainContext).finish();
              }).create();
      overlayPermissionDialog.setOnShowListener(dialogInterface -> {
        TextView message = overlayPermissionDialog.findViewById(android.R.id.message);
        if (message != null) {
          message.setTextColor(Color.BLACK);
        }
      });
      if(!MainActivity.this.isFinishing()){
          new Handler(Looper.getMainLooper()).postDelayed(() -> {
            if (!MainActivity.this.isFinishing() && overlayPermissionDialog != null && !overlayPermissionDialog.isShowing()) {
              new Handler(Looper.getMainLooper()).postDelayed(overlayPermissionRunnable, 1000);
            }
          }, 1000);
//        new Handler(Looper.getMainLooper()).postDelayed(overlayPermissionDialog::show, 500);
      }
    }
  }

as you can see, i added dismiss(); to close AlertDialog before calls finish(); but i still see the windowLeaked error from the logcat. i lost my last 3 days for this issues. please save my life.

here is the logcat log.

2023-11-27 11:15:13.775  5628-5628  MSHandlerLifeCycle      com.samho.app                        I  removeMultiSplitHandler: no exist. decor=DecorView@df904f2[MainActivity]
2023-11-27 11:15:13.777  5628-5685  OpenGLRenderer          com.samho.app                        D  setSurface called with nullptr
2023-11-27 11:15:13.777  5628-5628  ViewRootIm...nActivity] com.samho.app                        I  dispatchDetachedFromWindow
2023-11-27 11:15:13.780  5628-5628  InputTransport          com.samho.app                        D  Input channel destroyed: 'f49d4f7', fd=188
2023-11-27 11:15:13.782  5628-5628  WindowManager           com.samho.app                        E  android.view.WindowLeaked: Activity com.samho.app.MainActivity has leaked window DecorView@632e4c0[MainActivity] that was originally added here
                                                                                                        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:1329)
                                                                                                        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:1304)
                                                                                                        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:530)
                                                                                                        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:170)
                                                                                                        at android.app.Dialog.show(Dialog.java:521)
                                                                                                        at org.apache.cordova.inappbrowser.InAppBrowser$7.run(InAppBrowser.java:1128)
                                                                                                        at android.app.Activity.runOnUiThread(Activity.java:7560)
                                                                                                        at org.apache.cordova.inappbrowser.InAppBrowser.showWebPage(InAppBrowser.java:1137)
                                                                                                        at org.apache.cordova.inappbrowser.InAppBrowser$1.run(InAppBrowser.java:262)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:226)
                                                                                                        at android.os.Looper.loop(Looper.java:313)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8810)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
2023-11-27 11:15:13.783  5628-5685  OpenGLRenderer          com.samho.app                        D  setSurface called with nullptr
2023-11-27 11:15:13.789  5628-5628  chromium                com.samho.app                        I  [INFO:CONSOLE(393)] "%cnative %cApp.exitApp (#-1) font-weight: lighter; color: gray font-weight: bold; color: #000", source: capacitor-runtime.js (393)
2023-11-27 11:15:13.789  5628-5628  chromium                com.samho.app                        I  [INFO:CONSOLE(394)] "[object Object]", source: capacitor-runtime.js (394)
2023-11-27 11:15:13.790  5628-5628  chromium                com.samho.app                        I  [INFO:CONSOLE(395)] "console.groupEnd", source: capacitor-runtime.js (395)
2023-11-27 11:15:13.803  2236-4359  WindowManager           pid-2236                             E  setOnBackInvokedCallback(): No window state for package:com.samho.app
2023-11-27 11:15:13.804  5628-5628  MSHandlerLifeCycle      com.samho.app                        I  removeMultiSplitHandler: no exist. decor=DecorView@632e4c0[MainActivity]
2023-11-27 11:15:13.805  5628-5628  ViewRootIm...nActivity] com.samho.app                        I  dispatchDetachedFromWindow
2023-11-27 11:15:13.806  5628-5628  InputTransport          com.samho.app                        D  Input channel destroyed: '171a9d4', fd=238
2023-11-27 11:15:13.815  5628-5628  AndroidRuntime          com.samho.app                        D  Shutting down VM
2023-11-27 11:15:13.816  5628-5628  AndroidRuntime          com.samho.app                        E  FATAL EXCEPTION: main
                                                                                                    Process: com.samho.app, PID: 5628
                                                                                                    java.lang.IllegalArgumentException: View=DecorView@632e4c0[MainActivity] not attached to window manager
                                                                                                        at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:730)
                                                                                                        at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:616)
                                                                                                        at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:228)
                                                                                                        at android.app.Dialog.dismissDialog(Dialog.java:809)
                                                                                                        at android.app.Dialog.dismiss(Dialog.java:791)
                                                                                                        at org.apache.cordova.inappbrowser.InAppBrowser$6$1.onPageFinished(InAppBrowser.java:552)
                                                                                                        at WV.KY.b(chromium-TrichromeWebViewGoogle6432.aab-stable-604516333:12)
                                                                                                        at WV.D6.handleMessage(chromium-TrichromeWebViewGoogle6432.aab-stable-604516333:261)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:226)
                                                                                                        at android.os.Looper.loop(Looper.java:313)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8810)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
2023-11-27 11:15:14.517  5628-5628  Process                 com.samho.app                        I  Sending signal. PID: 5628 SIG: 9

i have searched a lot of same issue in here, but most of them say, to add dismiss(); before i call a finish(); method. so i did, but why its the same window leaked issue is still found..... i spent 3 days to fix this issues and still no success on that.

0

There are 0 best solutions below