nowinandroid
nowinandroid copied to clipboard
Fix - TopAppBar flickers when moving from destination to destination
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 .
@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.
- What was the fundamental problem?
- Is
rememberreally a fundamental solution rather than a temporary measure? - Are there any problems such as memory leaks by using
remember?
@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 .
Closing this PR as we have a fix for this: https://github.com/android/nowinandroid/pull/1728