DEV Community

myougaTheAxo
myougaTheAxo

Posted on

Accompanist Migration Guide — Official API Alternatives/Permissions/SystemUiController

What You'll Learn

Accompanist migration (Permissions→official API, SystemUiController→enableEdgeToEdge, Pager→HorizontalPager, Navigation Animation→official API) explained.


Why Accompanist is Deprecated

Most Accompanist features have migrated to official Compose. Since 2024, use official APIs in new projects instead of Accompanist.


Permissions

// ❌ Accompanist (deprecated)
// val permissionState = rememberPermissionState(Manifest.permission.CAMERA)

// ✅ Official ActivityResultContracts
@Composable
fun CameraPermissionScreen() {
    var hasPermission by remember { mutableStateOf(false) }

    val launcher = rememberLauncherForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { granted -> hasPermission = granted }

    LaunchedEffect(Unit) {
        launcher.launch(Manifest.permission.CAMERA)
    }

    if (hasPermission) {
        CameraContent()
    } else {
        Text("Camera permission required")
    }
}
Enter fullscreen mode Exit fullscreen mode

SystemUiController → enableEdgeToEdge

// ❌ Accompanist SystemUiController (deprecated)
// val systemUiController = rememberSystemUiController()
// systemUiController.setStatusBarColor(Color.Transparent)

// ✅ Official enableEdgeToEdge (Activity)
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        enableEdgeToEdge()  // Status/nav bar transparent
        super.onCreate(savedInstanceState)
        setContent {
            AppTheme {
                Scaffold(
                    modifier = Modifier.fillMaxSize(),
                    contentWindowInsets = WindowInsets(0)
                ) { innerPadding ->
                    Content(Modifier.padding(innerPadding))
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Pager → HorizontalPager

// ❌ Accompanist HorizontalPager (deprecated)
// HorizontalPager(count = 3, state = pagerState) { page -> ... }

// ✅ Official HorizontalPager (foundation)
@Composable
fun TabPager() {
    val pagerState = rememberPagerState(pageCount = { 3 })

    Column {
        TabRow(selectedTabIndex = pagerState.currentPage) {
            listOf("Tab 1", "Tab 2", "Tab 3").forEachIndexed { index, title ->
                Tab(
                    selected = pagerState.currentPage == index,
                    onClick = { /* coroutineScope.launch { pagerState.animateScrollToPage(index) } */ },
                    text = { Text(title) }
                )
            }
        }

        HorizontalPager(state = pagerState) { page ->
            Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                Text("Page ${page + 1}")
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Navigation Animation → Official

// ❌ Accompanist AnimatedNavHost (deprecated)
// AnimatedNavHost(navController, startDestination = "home") { ... }

// ✅ Official NavHost (animation built-in)
NavHost(navController, startDestination = "home") {
    composable(
        route = "home",
        enterTransition = { slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Left) },
        exitTransition = { slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Left) }
    ) { HomeScreen() }
}
Enter fullscreen mode Exit fullscreen mode

Summary

Accompanist Official Alternative
Permissions ActivityResultContracts
SystemUiController enableEdgeToEdge()
HorizontalPager foundation.pager
AnimatedNavHost navigation-compose
SwipeRefresh material3.PullToRefreshBox
FlowLayout foundation.FlowRow
  • New projects don't need Accompanist
  • Migrate existing projects gradually to official APIs
  • Most features integrated into foundation and material3

Ready-Made Android App Templates

8 production-ready Android app templates with Jetpack Compose, MVVM, Hilt, and Material 3.

Browse templatesGumroad

Top comments (0)