Initiate a Fresh Activity within Android using the Intent function from Jetpack Compose
Gotcha! Here's a fresh take on how to start a new activity using Intent in Android with Jetpack Compose:
Starting a New Activity in Android using Jetpack Compose, Simplified!
Let's Get Started
Step 1: Create a New Project in Android Studio
Follow the steps in Creating a New Project in Android Studio with Jetpack Compose if you haven't already.
Step 2: Create a New Activity
Create a new activity by right-clicking on the project folder, then "New > Activity > Gallery," and on the new window, select the "Empty Activity" option, and name it "SecondActivity."
Step 3: Work with the SecondActivity.kt File
Fill out the file with the following code:
```kotlinclass SecondActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Greeting("Geek!") } }}
@Composablefun Greeting(name: String) { Text( text = "Hello $name", style = MaterialTheme.typography.headlineMedium, modifier = Modifier.fillMaxSize() )}```
Step 4: Work with the MainActivity.kt File
Update the file with the following code:
```kotlinimport androidx.activity.compose.setContentimport androidx.compose.material3.Buttonimport androidx.compose.material3.Textimport androidx.compose.runtime.Composableimport androidx.compose.ui.platform.LocalContext
@Composablefun MainScreen() { val context = LocalContext.current
}
@ExperimentalMaterial3Apioverride fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MainScreen() }}```
Here's What Happens:
When you run the app, you'll see a button labeled "Go to Second Activity." Clicking this button will launch the , displaying the text "Hello Geek!" on the screen.
Now, learn more about starting a new activity with Intent from a Jetpack Compose composable within MainActivity in the next article.
Next Article Start a New Activity using Intent in Android using Jetpack Compose Kotlin* Android* Android-Jetpack
Technology plays a crucial role in this example, as it's the driving force behind the Android Studio environment and the Jetpack Compose library used for composing complex user interfaces with ease. Furthermore, the Intent is a core technology in Android for starting new activities and passing data between them, as demonstrated in this tutorial.