Я использую BottomNavigationView для MainActivity с 4 фрагментами.Кроме того, я использую LeakCanary для проверки утечек памяти, и я получил эту утечку фрагмента, хотя он не отсоединен и его хост-активность не уничтожена.
* 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;
}
}
В чем причина этой утечки памяти?