Cicero
04/29/2021, 5:02 PMrobnik
04/29/2021, 5:06 PMCicero
04/29/2021, 5:14 PMclass MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
navComposeApp()
}
}
}
@Composable
fun navComposeApp() {
val navController = rememberNavController()
val actions = remember(navController) { Action(navController) }
MaterialTheme() {
NavHost(
navController = navController,
startDestination = TEST_AREA
) {
composable(TEST_AREA) { TestArea(action = actions.testArea) }
}
}
}
object Destinations {
const val TEST_AREA = "TEST_AREA"
}
class Action(navController: NavHostController) {
val testArea: () -> Unit = { navController.navigate(TEST_AREA) }
val navigateBack: () -> Unit = { navController.popBackStack() }
}
@Composable
fun TestArea(action: () -> Unit) {}
Gradle Project:
classpath 'com.android.tools.build:gradle:7.0.0-alpha14'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
Gradle App:
dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.3.0-rc01'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.ui:ui-foundation:0.1.0-dev03'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation "androidx.navigation:navigation-compose:1.0.0-alpha10"
implementation "androidx.compose.ui:ui:1.0.0-beta05"
implementation "androidx.compose.material:material:1.0.0-beta05"
implementation "androidx.compose.ui:ui-tooling:1.0.0-beta05"
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Compose works fine until I try to drop navigation on it 😕
Navcomposer, inside of main activity set ContentCicero
04/29/2021, 5:16 PMrobnik
04/29/2021, 5:21 PMsetContent
? It's an extension function. If I jump to definition, the signature looks like this:
public fun ComponentActivity.setContent(
parent: CompositionContext? = null,
content: @Composable () -> Unit
) { ...
Chris Sinco [G]
04/29/2021, 5:41 PM=
instead of by
?
val actions = remember(navController) { Action(navController) }
Cicero
04/29/2021, 5:55 PMChris Sinco [G]
04/29/2021, 6:38 PMCicero
04/29/2021, 6:41 PMChris Sinco [G]
04/29/2021, 7:03 PMColor
and Modifier
🤦♂️Cicero
04/29/2021, 7:05 PMCicero
04/29/2021, 7:05 PMCicero
04/29/2021, 7:06 PMCicero
04/29/2021, 7:17 PMChris Sinco [G]
04/29/2021, 7:19 PMsetContent
or the IDE?Cicero
04/29/2021, 7:38 PMChris Sinco [G]
04/29/2021, 7:47 PMsetContent
?Cicero
04/29/2021, 7:50 PMCicero
04/29/2021, 7:50 PMAdam Powell
04/29/2021, 7:54 PMsetContent
?Cicero
04/29/2021, 8:03 PMCicero
04/29/2021, 8:03 PMfun Activity.setContent(
content: @Composable() () -> Unit
): CompositionContext? {
val composeView = window.decorView
.findViewById<ViewGroup>(android.R.id.content)
.getChildAt(0) as? AndroidComposeView
?: AndroidComposeView(this).also { setContentView(it) }
// If this value is inlined where it is used, an error that includes 'Precise Reference:
// kotlinx.coroutines.Dispatchers' not instance of 'Precise Reference: androidx.compose.Ambient'.
val coroutineContext = Dispatchers.Main
return Compose.composeInto(composeView.root, this) {
WrapWithAmbients(composeView, this, coroutineContext, content)
}
}
From ComponentActivity
public fun ComponentActivity.setContent(
parent: CompositionContext? = null,
content: @Composable () -> Unit
) {
val existingComposeView = window.decorView
.findViewById<ViewGroup>(android.R.id.content)
.getChildAt(0) as? ComposeView
if (existingComposeView != null) with(existingComposeView) {
setParentCompositionContext(parent)
setContent(content)
} else ComposeView(this).apply {
// Set content and parent **before** setContentView
// to have ComposeView create the composition on attach
setParentCompositionContext(parent)
setContent(content)
// Set the view tree owners before setting the content view so that the inflation process
// and attach listeners will see them already present
setOwners()
setContentView(this, DefaultActivityContentLayoutParams)
}
}
Cicero
04/29/2021, 8:04 PM@Composable invocations can only happen from the context of a @Composable function
Ian Lake
04/29/2021, 8:07 PMandroidx.ui:ui-foundation:0.1.0-dev03
dependency - you shouldn't be using any 0.1.0-dev
version of Compose artifacts anymore. Removing that will also remove the ambiguityCicero
04/29/2021, 8:10 PMIan Lake
04/29/2021, 8:17 PMCicero
04/29/2021, 8:20 PMCicero
04/29/2021, 10:28 PM