From 2ecde80fbdbb4f4f2dd031b956fdb19f857ddd63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 22 Nov 2019 14:34:15 +0800 Subject: [PATCH] fix memory leak --- .../app/src/main/java/pub/doric/demo/DemoActivity.java | 9 +++++---- .../app/src/main/java/pub/doric/demo/MainActivity.java | 6 ------ .../src/main/java/pub/doric/engine/DoricJSEngine.java | 6 +++++- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java index 4c773d7c..31d88f4b 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -22,6 +22,7 @@ import android.widget.FrameLayout; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -41,7 +42,6 @@ import pub.doric.utils.DoricUtils; */ public class DemoActivity extends AppCompatActivity { private DoricContext doricContext; - private DevPanel devPanel = new DevPanel(); private SensorManagerHelper sensorHelper; @Override @@ -59,10 +59,11 @@ public class DemoActivity extends AppCompatActivity { sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() { @Override public void onShake() { - if (devPanel.isAdded()) { + Fragment devPanel = getSupportFragmentManager().findFragmentByTag("DevPanel"); + if (devPanel != null && devPanel.isAdded()) { return; } - devPanel.show(getSupportFragmentManager(), "DevPanel"); + new DevPanel().show(getSupportFragmentManager(), "DevPanel"); } }); } @@ -107,7 +108,7 @@ public class DemoActivity extends AppCompatActivity { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) { - devPanel.show(getSupportFragmentManager(), "DevPanel"); + new DevPanel().show(getSupportFragmentManager(), "DevPanel"); } return super.onKeyDown(keyCode, event); } diff --git a/Android/app/src/main/java/pub/doric/demo/MainActivity.java b/Android/app/src/main/java/pub/doric/demo/MainActivity.java index 6ba6f31e..59a6ce2d 100644 --- a/Android/app/src/main/java/pub/doric/demo/MainActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/MainActivity.java @@ -32,11 +32,9 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import pub.doric.dev.DevPanel; import pub.doric.utils.DoricUtils; public class MainActivity extends AppCompatActivity { - private DevPanel devPanel = new DevPanel(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -91,10 +89,6 @@ public class MainActivity extends AppCompatActivity { tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (position == 0) { - devPanel.show(getSupportFragmentManager(), "DevPanel"); - return; - } Intent intent = new Intent(tv.getContext(), DemoActivity.class); intent.putExtra("source", data[position]); tv.getContext().startActivity(intent); diff --git a/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index 2bf520bf..64a3dbad 100644 --- a/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -40,6 +40,8 @@ import pub.doric.utils.DoricUtils; * @CreateDate: 2019-07-18 */ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback { + + private HandlerThread handlerThread; private final Handler mJSHandler; private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension(); private IDoricJSE mDoricJSE; @@ -47,7 +49,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time private final DoricRegistry mDoricRegistry = new DoricRegistry(); public DoricJSEngine(final boolean isNative, final IStatusCallback statusCallback) { - HandlerThread handlerThread = new HandlerThread(this.getClass().getSimpleName()); + handlerThread = new HandlerThread(this.getClass().getSimpleName()); handlerThread.start(); Looper looper = handlerThread.getLooper(); mJSHandler = new Handler(looper, this); @@ -171,6 +173,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time public void teardown() { mDoricJSE.teardown(); mTimerExtension.teardown(); + handlerThread.quit(); + mJSHandler.removeCallbacksAndMessages(null); } private void loadBuiltinJS(String assetName) {