fix memory leak

This commit is contained in:
王劲鹏 2019-11-22 14:34:15 +08:00
parent f1da548fb8
commit 2ecde80fbd
3 changed files with 10 additions and 11 deletions

View File

@ -22,6 +22,7 @@ import android.widget.FrameLayout;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
@ -41,7 +42,6 @@ import pub.doric.utils.DoricUtils;
*/ */
public class DemoActivity extends AppCompatActivity { public class DemoActivity extends AppCompatActivity {
private DoricContext doricContext; private DoricContext doricContext;
private DevPanel devPanel = new DevPanel();
private SensorManagerHelper sensorHelper; private SensorManagerHelper sensorHelper;
@Override @Override
@ -59,10 +59,11 @@ public class DemoActivity extends AppCompatActivity {
sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() { sensorHelper.setOnShakeListener(new SensorManagerHelper.OnShakeListener() {
@Override @Override
public void onShake() { public void onShake() {
if (devPanel.isAdded()) { Fragment devPanel = getSupportFragmentManager().findFragmentByTag("DevPanel");
if (devPanel != null && devPanel.isAdded()) {
return; return;
} }
devPanel.show(getSupportFragmentManager(), "DevPanel"); new DevPanel().show(getSupportFragmentManager(), "DevPanel");
} }
}); });
} }
@ -107,7 +108,7 @@ public class DemoActivity extends AppCompatActivity {
@Override @Override
public boolean onKeyDown(int keyCode, KeyEvent event) { public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) { if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) {
devPanel.show(getSupportFragmentManager(), "DevPanel"); new DevPanel().show(getSupportFragmentManager(), "DevPanel");
} }
return super.onKeyDown(keyCode, event); return super.onKeyDown(keyCode, event);
} }

View File

@ -32,11 +32,9 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import pub.doric.dev.DevPanel;
import pub.doric.utils.DoricUtils; import pub.doric.utils.DoricUtils;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private DevPanel devPanel = new DevPanel();
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -91,10 +89,6 @@ public class MainActivity extends AppCompatActivity {
tv.setOnClickListener(new View.OnClickListener() { tv.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (position == 0) {
devPanel.show(getSupportFragmentManager(), "DevPanel");
return;
}
Intent intent = new Intent(tv.getContext(), DemoActivity.class); Intent intent = new Intent(tv.getContext(), DemoActivity.class);
intent.putExtra("source", data[position]); intent.putExtra("source", data[position]);
tv.getContext().startActivity(intent); tv.getContext().startActivity(intent);

View File

@ -40,6 +40,8 @@ import pub.doric.utils.DoricUtils;
* @CreateDate: 2019-07-18 * @CreateDate: 2019-07-18
*/ */
public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback { public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback {
private HandlerThread handlerThread;
private final Handler mJSHandler; private final Handler mJSHandler;
private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension(); private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension();
private IDoricJSE mDoricJSE; private IDoricJSE mDoricJSE;
@ -47,7 +49,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
private final DoricRegistry mDoricRegistry = new DoricRegistry(); private final DoricRegistry mDoricRegistry = new DoricRegistry();
public DoricJSEngine(final boolean isNative, final IStatusCallback statusCallback) { public DoricJSEngine(final boolean isNative, final IStatusCallback statusCallback) {
HandlerThread handlerThread = new HandlerThread(this.getClass().getSimpleName()); handlerThread = new HandlerThread(this.getClass().getSimpleName());
handlerThread.start(); handlerThread.start();
Looper looper = handlerThread.getLooper(); Looper looper = handlerThread.getLooper();
mJSHandler = new Handler(looper, this); mJSHandler = new Handler(looper, this);
@ -171,6 +173,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
public void teardown() { public void teardown() {
mDoricJSE.teardown(); mDoricJSE.teardown();
mTimerExtension.teardown(); mTimerExtension.teardown();
handlerThread.quit();
mJSHandler.removeCallbacksAndMessages(null);
} }
private void loadBuiltinJS(String assetName) { private void loadBuiltinJS(String assetName) {