Home > Software design >  Navigation jetpack compose doesn't show the screens that you put inside it
Navigation jetpack compose doesn't show the screens that you put inside it

Time:01-24

I have a problem with navigation jetpack compose, the problem is when I run the app on my phone, there is no screen just an empty screen but if I do a long click inside the empty screen after the long click the screen that I put in navigation display.

what is the problem and how can I fix it?

package com.example.navigationappfirst

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.navigationappfirst.ui.theme.NavigationAppFirstTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NavigationAppFirstTheme {
                val navController = rememberNavController()
                NavHost(navController = navController, startDestination = "younes1") {
                    composable(route = "younes1") {
                        NoteScreen(text = "younes1", navController)
                    }
                    composable(route = "younes2") {
                        NoteScreen(text = "younes2", navController)
                    }

                }
            }
        }
    }
}

package com.example.navigationappfirst

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavController

@Composable
fun NoteScreen(text: String, navController: NavController) {

    Column(
        modifier = Modifier.fillMaxSize()
    ) {

        Button(onClick = {
            if (text.equals("younes1")) navController.navigate("younes2")
            else navController.navigate("younes1")
        }) {
            Text(
                text = text,
                modifier = Modifier.fillMaxWidth()
            )
        }
        
    }

}

def nav_version = "2.5.3"
implementation "androidx.navigation:navigation-compose:$nav_version"

CodePudding user response:

First of all, The button is showing fine at the top of my emulator. Are u using real device or emulator ?. You may try change the column in your NoteScreen to something like

  • Related