Skip to main content
  1. About
  2. Stack Internal
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

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()
 startActivity(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)

Answer*

Draft saved
Draft discarded

Required fields are marked with *

Cancel
3
  • Maybe because FragmentContainerView is child of FrameLayout and it is just placed with in the activity.xml as i have posted and android OS is responsible for all inflation process Commented Mar 26, 2023 at 12:26
  • @AbdallaMaged So, where did that R.id.null FrameLayout came from? It looks like that framelayout is causing the leak. I need more detail about that R.id.null view. Commented Mar 26, 2023 at 13:49
  • Where can it be , as I have posted, the activity xml has nothing more than FragmentContainer and also I Have Removed all subviews before moving to next activity Commented Mar 27, 2023 at 22:42

lang-kotlin

AltStyle によって変換されたページ (->オリジナル) /