gpt4 book ai didi

android - Leakcanary 使用 AppCompatActivity 和 FragmentStatePagerAdapter 显示 fragment 泄漏

转载 作者:太空宇宙 更新时间:2023-11-03 13:53:08 26 4
gpt4 key购买 nike

这个问题我已经有一段时间了,我真的不知道还能尝试什么。我在我的项目中加入了 Leakcanary,这样我就可以强制自己“以正确的方式”学习 android,而不会选择导致内存泄漏的模式。我正在构建一个应用程序,我已经将它剥离了很多,但我仍然看到这种泄漏正在发生。

我有一个 Activity,里面有一个 viewpager 并实例化了 10 个 fragment 。如果我打开设备,一切似乎都很好,直到我按下“Home”,我才得到以下内存泄漏:

D/LeakCanary﹕ In com.doesnthaveadomain.leo.calendartracker:1.0:1.
D/LeakCanary﹕ * com.doesnthaveadomain.leo.calendartracker.MyFragment has leaked:
D/LeakCanary﹕ * GC ROOT static android.view.WindowManagerGlobal.sDefaultWindowManager
D/LeakCanary﹕ * references android.view.WindowManagerGlobal.mRoots
D/LeakCanary﹕ * references java.util.ArrayList.array
D/LeakCanary﹕ * references array java.lang.Object[].[0]
D/LeakCanary﹕ * references android.view.ViewRootImpl.mInvalidateOnAnimationRunnable
D/LeakCanary﹕ * references android.view.ViewRootImpl$InvalidateOnAnimationRunnable.mViews
D/LeakCanary﹕ * references java.util.ArrayList.array
D/LeakCanary﹕ * references array java.lang.Object[].[0]
D/LeakCanary﹕ * references android.support.v4.view.ViewPager.mAdapter
D/LeakCanary﹕ * references com.doesnthaveadomain.leo.calendartracker.MyAdapter.mCurrentPrimaryItem
D/LeakCanary﹕ * leaks com.doesnthaveadomain.leo.calendartracker.MyFragment instance

主要 Activity :

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), this);
mViewPager.setAdapter(myAdapter);
mViewPager.setCurrentItem(5);
}

}

class MyAdapter extends FragmentStatePagerAdapter {

private int mNumberOfViews;
private final WeakReference<AppCompatActivity> mActivityWeakRef;

public MyAdapter(FragmentManager fm,
AppCompatActivity activity) {
super(fm);

mActivityWeakRef = new WeakReference<AppCompatActivity>(activity);
mNumberOfViews = 10;
}

@Override
public Fragment getItem(int position) {
MyFragment myFragment = new MyFragment();
AppCompatActivity activity = mActivityWeakRef.get();
if (activity != null) {
MyApp.getRefWatcher(mActivityWeakRef.get()).watch(myFragment);
}
return myFragment;
}

@Override
public int getCount() {
return mNumberOfViews;
}
}

主要 Activity 布局:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true" >

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:theme="@style/AppTheme.Toolbar"
app:popupTheme="@style/AppTheme.Toolbar.Popup">

</android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

fragment :

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* A placeholder fragment containing a simple view.
*/
public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_test, container, false);
}

}

fragment 布局:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_vertical_margin"
android:paddingStart="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_vertical_margin"
android:paddingEnd="@dimen/activity_vertical_margin"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

</LinearLayout>

最佳答案

与我的其他问题相同,我在错误的时间初始化 ref watcher,在 fragment 创建时而不是在销毁时:https://stackoverflow.com/a/32881096/5278339

关于android - Leakcanary 使用 AppCompatActivity 和 FragmentStatePagerAdapter 显示 fragment 泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32749332/

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