Hi here ! I'm developing an app with compose, I'm ...
# compose
l
Hi here ! I'm developing an app with compose, I'm facing an issue that my view is reloading the content twice After investigation it's look like my VM is created twice and do the call that change my data to display I user compose navigation based on a fun where the content can change Can someone help me with this ?
đź§µ 1
Here is my code
Copy code
fun currentRoles() : Roles {
        return currentRole
    }

fun defineRole() {
        currentRole = if (!hasUserCS) {
            if (!hasPet) {
                nextRoles.add(Roles.Pet)
            }
            if (!hasPetCS) {
                nextRoles.add(Roles.PetCS)
            }
            nextRoles.add(Roles.FullCS)
            Roles.UserCS
        } else if (!hasPet) {
            if (!hasPetCS) {
                nextRoles.add(Roles.PetCS)
            }
            nextRoles.add(Roles.FullCS)
            Roles.Pet
        } else if (!hasPetCS) {
            nextRoles.add(Roles.PetCS)
            nextRoles.add(Roles.FullCS)
            Roles.Pet
        } else {
            Roles.FullCS
        }
    }

// My navGraph for the App
fun RootNavGraph(
    
) {
    val navController = rememberNavController()
    var mUser : User? = null
    var mPet by remember { mutableStateOf<Pet?>(null) }
    var target by remember { mutableStateOf(Roles.Guest) }

    NavHost(
        navController = navController,
        route = RootRoute.route,
        startDestination = when (roleService.currentRoles()) {
            Roles.Guest, Roles.Country -> {
                ExplorerRoute.create(RootRoute.route, WelcomeRoute)
            }
            Roles.FullCS -> {
                if (ContextCompat.checkSelfPermission(activity, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED || isHiddenFor2Weeks()) {
                    ExplorerRoute.create(RootRoute.route, AppRoute)
                } else {
                    ExplorerRoute.create(RootRoute.route, RequestNotificationRoute)
                }
            }
            else -> {
                if (userService.currentUser.id != 0) mUser = toNewUser(userService.currentUser)
                if (petService.currentPet.id != 0) mPet = petService.currentPet
                target = roleService.currentRoles()
                ExplorerRoute.create(RootRoute.route, JoinRoute)
            }
        }
z
I’m not entirely sure what the issue you’re seeing is. Recomposition can happen for a whole bunch of reasons — a composable recomposing once or twice more than you think isn’t a problem.
mUser
is suspicious, since it’s not a
remember { mutableStateOf() }
. The
m
prefix for fields is typically not used in modern code, and even if it is in a code base, it stands for “member” and variables defined in functions aren’t class members so it’s not accurate anyway. If
defineRoles
is called from a composable that’s probably bad, since it adds to a list and so it’ll add multiple times when recomposed.