Creating the MainActivity
To show the new screen, we need to set up our MainActivity class. Open MainActivity.kt and update it with following code. This code sets up our MainActivity with a bottom navigation bar, including a User screen.
Kotlin
package <YOUR_ROOT_PACKAGE_HERE>
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.compose.runtime.*import androidx.compose.foundation.layout.*import androidx.compose.material.icons.Iconsimport androidx.compose.material.icons.filled.Personimport androidx.compose.material3.*import androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport io.appwrite.models.Userimport <YOUR_ROOT_PACKAGE_HERE>.services.Appwriteimport <YOUR_ROOT_PACKAGE_HERE>.ui.screens.UserScreenimport <YOUR_ROOT_PACKAGE_HERE>.services.AccountService
enum class Screen { User}
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
Appwrite.init(applicationContext)
setContent { AppContent(Appwrite.account) } }}
@Composableprivate fun AppBottomBar(screen: MutableState<Screen>) { BottomAppBar { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center ) { IconButton(onClick = { screen.value = Screen.User }) { Column(horizontalAlignment = Alignment.CenterHorizontally) { Icon(Icons.Default.Person, contentDescription = "User") Text("User") } } } }}
@OptIn(ExperimentalMaterial3Api::class)@Composableprivate fun AppContent(accountService: AccountService) { val user = remember { mutableStateOf<User<Map<String, Any>>?>(null) } val screen = remember { mutableStateOf(Screen.User) }
LaunchedEffect(screen) { user.value = accountService.getLoggedIn() }
Scaffold(bottomBar = { AppBottomBar(screen) }) { padding -> Column(modifier = Modifier.padding(padding)) { when (screen.value) { Screen.User -> UserScreen(user, accountService) else -> Text("Ideas screen") } } }}Test the MainActivity
Launch the app and you should be able to use the Login screen to register, login, and logout. Confirm your email address is displayed once you are logged in.
Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.