Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - TopAppBar flickers when moving from destination to destination #1686

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mahmed1987
Copy link

@mahmed1987 mahmed1987 commented Nov 7, 2024

Issue
currentTopLevelDestination is null momentarily , causing the TopAppBar to momentarily go out of the composition tree while navigating the top level destinations.
In short the problem lies here

val currentTopLevelDestination: TopLevelDestination?
        @Composable get() {
            return TopLevelDestination.entries.firstOrNull { topLevelDestination ->
                Log.d("currentDestination","$currentDestination")
                currentDestination?.hasRoute(route = topLevelDestination.route) ?: false
            }
        }

The log statement above would confirm that while switching between the top level composables , the currentDestination is obtained as null at times , until it is not .

I dont have the exact reasoning as to why that is. My hypothesis is that since there is no "gate" between the currentDestination composable and the currentTopLevelDestination , currentTopLevelDestination reacts to everything that occurs to currentDestination and perhaps currentDestination goes through a null while going from one destination to another.

Solution

I just gated the currentTopLevelDestination with a remember .

Before
After

@mahmed1987 mahmed1987 changed the title TopAppBar flickers when moving from destination to destination Fix - TopAppBar flickers when moving from destination to destination Nov 7, 2024
@Songgyubin
Copy link
Contributor

Songgyubin commented Nov 20, 2024

@mahmed1987
I have confirmed that the problem you mentioned has been resolved.

However, I think there is something that needs more thought about merging into the code.

  1. What was the fundamental problem?
  2. Is remember really a fundamental solution rather than a temporary measure?
  3. Are there any problems such as memory leaks by using remember?

@mahmed1987
Copy link
Author

mahmed1987 commented Nov 20, 2024

@Songgyubin tbh I am not fully certain what is at play here.

Like I wrote in the PR description , this appears to be some situation that is occurring perhaps because of the compose state currentDestination is being read inside of a firstOrNull lambda ?

This appears to be a temporary fix until an explanation has been made available by the compose team .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants