gpt4 book ai didi

java - 尝试检测 ActionMode 内存泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:41:26 25 4
gpt4 key购买 nike

几天来我一直在尝试寻找 ActionMode 内存泄漏的根源,但没有成功。我有一个包含多个 fragment 的 Activity ,当我离开具有 ActionMode 的 fragment (同时自动取消它)时,LeakCanary 检测到内存泄漏。

我已经在 destroy() 上取消了 ActionMode 和 ActionMode.Callback,甚至尝试在 onDestroyActionMode() 上这样做。

这是我的 LeakCanary 屏幕截图:

https://i.imgur.com/RUbdqj3.png

我希望有人能给我指出正确的方向。

附言我怀疑它与 ActionMode.Callback 有关。虽然,我找不到任何破坏它的 CallBack 方法。我使用 startSupportActionMode(mActionModeCallback) 启动 ActionMode。我也试图找到一种方法来从中删除 mActionModeCallback,但没有方法。

这是我完整的 ActionMode 代码:

private ActionMode mActionMode;
private ActionMode.Callback mActionModeCallback;

public void startCAB()
{
if (mActionMode == null)
mActionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(mActionModeCallback);
}


private void buildActionModeCallBack()
{
mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_cab, menu);
return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
... Some Code ...
}
}

@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
mActionModeCallback = null; // Tried with and without this.
}
};
}

public void finishActionMode()
{
mActionMode.finish();
}

@Override
public void onDestroy()
{
super.onDestroy();
mActionMode = null;
mActionModeCallback = null;
}

包含 fragment 的父 Activity :

@Override
public void onTabUnselected(TabLayout.Tab tab)
{
clearCAB();
}

private void clearCAB()
{
int index = mPagerAdapter.getCurrentFragmentIndex();
FragmentOne fragmentOne = (FragmentOne) mPagerAdapter.instantiateItem(mViewPager, index);
fragmentOne.finishActionMode();
}

最佳答案

根据我的经验,如果你的ActionMode.Callback对象使用了匿名内部类,它可能会导致你的 fragment 内存泄漏。

也许你可以创建一个新类并实现 ActionMode.Callback 然后用它来放入 startSupportActionMode() 参数:

public class YourFragment extends skip implements skip, ActionMode.Callback {

private ActionMode mActionMode;

public void startCAB()
{
if (mActionMode == null)
mActionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(new SafeActionModeCallback(this));
}

public void finishActionMode()
{
mActionMode.finish();
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_cab, menu);
return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
// ... Some Code ...
}
}

@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
}

安全操作模式回调:

public class SafeActionModeCallback implements ActionMode.Callback {

// you can also use the WeakReference
private ActionMode.Callback callback;

public SafeActionModeCallback(ActionMode.Callback callback) {
this.callback = callback;
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return callback.onCreateActionMode(mode, menu);
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return callback.onPrepareActionMode(mode, menu);
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return callback.onActionItemClicked(mode, item);
}

@Override
public void onDestroyActionMode(ActionMode mode) {
callback.onDestroyActionMode(mode);
callback = null;
}
}

关于java - 尝试检测 ActionMode 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53261839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com