Skip to content

previous activity cannot be garbage collected because DebugMenu keeps old reference #10

Description

@timacostadev

Problem:

According to the README.md - "If your project is a single activity but uses Fragments for navigation, you can use the DebugMenuAttacher.attach method to your Activity.onCreate() to attach the debug menu to your whole application."

If the activity gets recreated for any reason, the old activity cannot be garbage collected because the previous reference doesn't allow it. Creating multiple activity instances and in our specific case, an unresponsive app.

DebugMenuAttacher.attach(
            activity,

attach(activity) creates a ComposeView(activity) and adds it to the activity's decor view. Compose's internal AndroidComposeView retains a strong reference to the Activity context, apparently.
I tested around a bit locally with the library and adding a flag "fixed" the issue, and kind of confirmed that it is related.

@JvmStatic
    fun attach(
        activity: Activity,
        modules: List<DebugMenuModule>,
        showFab: Boolean = true,
        enableShake: Boolean = false,
    ) = runCatching {
        if (attached) return@runCatching
        attached = true

Reproduction: Call DebugMenuAttacher.attach(activity) in onCreate() of a single-activity app. Switch dark/light mode twice via AppCompatDelegate.setDefaultNightMode().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions