How to pass a value properly for a function called in MainActivity?

55 Views Asked by At

I wanted to use a navigation file in which I specify route for 2 different pages, and use a TopAppBar to switch from one to an other.

On my TopAppBar I passed

'myNavController: NavHostController'

as a parameter and then used

'myNavController.navigate(route = "")'

into onClick value parameters.

This TopAppBar and a box in which I want to display my different pages (function called MyNavigation() in the below code) are placed into an Interface function that I finally call into 'MainActivity.kt'.

Here is my Interface code:

@Composable
fun Interface(modifier: Modifier = Modifier, myNavController: NavHostController){
    Column(
        modifier = Modifier
            .fillMaxWidth()
    ) {
        TopAppBar(myNavController)

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(10.dp)
        ) {
            MyNavigation()
        }
    }
}

Everything seems fine so far, my problem being I can't figure out how to properly pass the parameter

'myNavController'

when I call 'Interface()' into 'MainActivity.kt'

as shown below:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            UITest_1Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Interface(myNavController: NavHostController)
                }
            }
        }
    }
}

I tried to pass it as simply as the other times with 'myNavController: NavHostController'

and obtain this problem description: Classifier 'NavHostController' does not have a companion object, and thus must be initialized here

0

There are 0 best solutions below