Easier to complete Jetpack Compose navigation
In your build.gradle
:
dependencies {
implementation 'com.patchself:compose-navigator:0.1.3'
}
Or if you use gradle.kts, in yourbuild.gradle.kts
dependencies {
implementation("com.patchself:compose-navigator:0.1.3")
}
- Add config your compose activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//set open app first page
navigationController.initController(SplashPage())
setContent {
navigationController.viewContent()
}
}
override fun onBackPressed() {
// navigator go back handler
if (!navigationController.onBackPressed()){
super.onBackPressed()
}
}
}
- Write your custom page
class CustomPage : PageController() {
override fun getId() = R.id.CustomPage
/**
* write page content in this method
*/
@Composable
override fun screenContent() {
Scaffold(topBar = {
TopAppBar(
title = { Text(text = "Navigator Sample") },
navigationIcon = {
IconButton(onClick = { navigateBack() }) {
Icon(asset = Icons.Filled.ArrowBack)
}
},
elevation = 4.dp
)
}) {
}
}
}
- In your Custom Page,which extend
PageController
, you can usenavigateTo(PageController)
to new page, andnavigateBack()
go back to last page - You can use
resetTo(pageId)
back to specify page, and change page args likeresetTo<HomePage>(R.id.HomePage){ argsOfHomePage = newValue }