I am using a custom Backstack to manage the Application screens. Basically, the custom BackStack is a List. The problem is that this method does memory leaks.
This method checks if there is a Fragment in the cell already:
private fun addToBackStack(fragment: Fragment){
try {
if (backStack.isNotEmpty() && backStack[backStackPosition] != null)
fragmentUtils.removeFragment(backStack[backStackPosition]!!)
} catch (e: IndexOutOfBoundsException) {
e.printStackTrace()
}
backStack.add(backStackPosition, fragment)
}
And this is how I try to remove it:
fun removeFragment(fragment: Fragment?){
fm.beginTransaction().remove(fragment).commit()
}
But I still have memory leaks, according to LeakCanary. What am I doing wrong, or how to completely destroy a Fragment?