Activity with Navigation component cause A Memory Leak after Start New Root Activity or Calling Finish()
My app start with this Activity _ A _ Which has FragmentContainer that get initialized as following
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/register_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
tools:layout="@layout/fragment_login"
tools:ignore="FragmentTagUsage" />
</androidx.constraintlayout.widget.ConstraintLayout>
private var navHostFragment: NavHostFragment? = null
private var navController: NavController? = null
private fun setNavGraph() {
navHostFragment = supportFragmentManager.findFragmentById(R.id.register_nav_host_fragment) as NavHostFragment
navController = navHostFragment?.navController
val graph = navController?.navInflater?.inflate(R.navigation.registration_process_graph)
navController?.setGraph(graph !!, intent.extras)
}
now when i try to open new Activity as a root i got a Memory Leak
val intent = Intent(this, DashboardActivity::class.java)
intent.putExtra(AppConstants.EXTRA_MODEL, getViewModel().getDashboardResponse())
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
releaseInstance()
openActivity(intent)
┬───
│ GC Root: System class
│
├─ android.transition.TransitionManager class
│ Leaking: NO (a class is never leaking)
│ ↓ static TransitionManager.sPendingTransitions
│ ~~~~~~~~~~~~~~~~~~~
├─ java.util.ArrayList instance
│ Leaking: UNKNOWN
│ Retaining 294.4 kB in 5955 objects
│ ↓ ArrayList[0]
│ ~~~
├─ android.widget.FrameLayout instance
│ Leaking: YES (View.mContext references a destroyed activity)
│ Retaining 89.6 kB in 1750 objects
│ View is part of a window view hierarchy
│ View.mAttachInfo is null (view detached)
│ View.mID = R.id.null
│ View.mWindowAttachCount = 0
│ mContext instance of android.view.ContextThemeWrapper, wrapping activity
│ com.demo.bm.auth.ui.AuthActivity with mDestroyed = true
│ ↓ View.mContext
├─ android.view.ContextThemeWrapper instance
│ Leaking: YES (FrameLayout↑ is leaking and ContextThemeWrapper wraps an
│ Activity with Activity.mDestroyed true)
│ Retaining 48 B in 2 objects
│ mBase instance of com.demo.bm.auth.ui.AuthActivity with mDestroyed =
│ true
│ ↓ ContextWrapper.mBase
╰→ com.demo.bm.auth.ui.AuthActivity instance
Leaking: YES (ObjectWatcher was watching this because com.demo.bm.
auth.ui.AuthActivity received Activity#onDestroy() callback and
Activity#mDestroyed is true)
Retaining 30.6 kB in 971 objects
key = 9dec9a77-29b1-46a6-a8ef-143f2811a4d1
watchDurationMillis = 5590
retainedDurationMillis = 575
mApplication instance of com.demo.bm.demoApp
mBase instance of androidx.appcompat.view.ContextThemeWrapper
Kindly note that i have tried to remove all subViews and cleared local variables of Activity _ A _ before moving to DashboardActivity as following
navHostFragment = null
navController = null
val navView = findViewById<FrameLayout>(R.id.register_nav_host_fragment)
(navView.parent as ViewManager).removeView(navView)
Abdalla Maged
- reputation score 55
- 1 silver badge
- 6 bronze badges
lang-kotlin