nowinandroid icon indicating copy to clipboard operation
nowinandroid copied to clipboard

Fix - TopAppBar flickers when moving from destination to destination

Open mahmed1987 opened this issue 1 year ago • 2 comments

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 avatar Nov 07 '24 08:11 mahmed1987

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

Songgyubin avatar Nov 20 '24 14:11 Songgyubin

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

mahmed1987 avatar Nov 20 '24 15:11 mahmed1987

Closing this PR as we have a fix for this: https://github.com/android/nowinandroid/pull/1728

trambui09 avatar Dec 11 '24 18:12 trambui09