1

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)
asked Mar 26, 2023 at 11:03

1 Answer 1

0

Your Leak Canary log looks werid.

├─ 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

FrameLayout is detached but it is also part of a window view hierarchy. How that could be possible?. I'm not sure but did you add FrameLayout directly to the window or used WindowManager like this?

// using window API
window.addContentView(FrameLayout(this), null)
// using acitivity API
[email protected](FrameLayout(this), null)
// using WindowManager API
windowManager.addView(FrameLayout(this), ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))
answered Mar 26, 2023 at 12:13
Sign up to request clarification or add additional context in comments.

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
@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.
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.