我在MainActivity上使用带有4个片段的BottomNavigationView。另外,我使用LeakCanary检查内存泄漏,虽然没有分离并且它的托管活动没有销毁,但我收到了这个Fragment泄漏。
* org.kafora.apps.qutoof.ui.main.home.HomeFragment has leaked:
* static ActivityThread.sCurrentActivityThread
* ↳ ActivityThread.mActivities
* ↳ ArrayMap.mArray
* ↳ array Object[].[3]
* ↳ ActivityThread$ActivityClientRecord.activity
* ↳ MainActivity.!(mHomeFragment)!
* ↳ HomeFragment
泄漏跟踪详细信息: https://drive.google.com/file/d/1Kd82el7z_82AVLfYDrhfEW7tV50kcrQP/view?usp=sharing
这是MainActivity
类:
public class MainActivity extends MediaBaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener
, BottomNavigationView.OnNavigationItemReselectedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private FragmentManager fragmentManager;
private BrowseFragment mBrowseFragment;
private HomeFragment mHomeFragment;
private LibraryFragment mLibraryFragment;
private ReminderFragment mReminderFragment;
private Unbinder unbinder;
@BindView(R.id.parentView)
View parentView;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.bottom_nav_view) BottomNavigationView bottomNavView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unbinder = ButterKnife.bind(this);
fragmentManager = getSupportFragmentManager();
displayInitialHomeFragment();
setSupportActionBar(toolbar);
setupBottomNavigationView();
}
private HomeFragment getHomeFragment(){
if(mHomeFragment == null) mHomeFragment = HomeFragment.newInstance();
return mHomeFragment;
}
private BrowseFragment getBrowseFragment(){
if(mBrowseFragment == null) mBrowseFragment = BrowseFragment.newInstance();
return mBrowseFragment;
}
private LibraryFragment getLibraryFragment(){
if(mLibraryFragment == null) mLibraryFragment = LibraryFragment.newInstance();
return mLibraryFragment;
}
private ReminderFragment getReminderFragment(){
if(mReminderFragment == null) mReminderFragment = ReminderFragment.newInstance();
return mReminderFragment;
}
private void displayInitialHomeFragment() {
if (fragmentManager.findFragmentById(R.id.frame_container) == null) {
fragmentManager.beginTransaction().add(R.id.frame_container, getHomeFragment()).commit();
}
}
private void displaySelectedFragment(Fragment fragment) {
if (fragment != fragmentManager.findFragmentById(R.id.frame_container)) {
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment)
.commit();
}
}
public void setupBottomNavigationView() {
bottomNavView.setOnNavigationItemSelectedListener(this);
bottomNavView.setOnNavigationItemReselectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.home:
displaySelectedFragment(getHomeFragment());
return true;
case R.id.browse:
displaySelectedFragment(getBrowseFragment());
return true;
case R.id.library:
displaySelectedFragment(getLibraryFragment());
return true;
case R.id.reminder:
displaySelectedFragment(getReminderFragment());
return true;
default:
return false;
}
}
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
}
@Override
protected void onDestroy() {
super.onDestroy();
unbinder.unbind();
mHomeFragment= null;
mBrowseFragment = null;
mReminderFragment = null;
mLibraryFragment = null;
}
}
此内存泄漏的原因是什么?