Open In App

Start a New Activity using Intent in Android using Jetpack Compose

Last Updated : 01 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Android OS, an Intent is a mechanism used to navigate a user to another activity or application to achieve a specific task. In general, Intents are used to progress to the next activity or come back to the previous activity.

Intent In Android

 

In this article, we will show you how you could start a New Activity using Intent in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Create a New Activity SecondActivity.kt

Create a new activity by right-clicking on the project folder, click on new, click on Activity, and select the Gallery option. In the Gallery option, select Empty Compose Activity and name it SecondActivity as shown in the below images.

 

 

Step 3: Working with SecondActivity.kt file

Open the SecondActivity.kt file and refer to the following code. Below is the code for the SecondActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcintentnextactivity
  
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
  
class SecondActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // Calling the composable function
            // to display element and its contents
            MainContent2()
        }
    }
}
  
// Creating a composable
// function to display Top Bar
@Composable
fun MainContent2() {
    Scaffold(
        topBar = { TopAppBar(title = { Text("GFG | Second Activity", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
        content = { MyContent2() }
    )
}
  
// Creating a composable function to 
// create two Images and a spacer between them
// Calling this function as content in the above function
@Composable
fun MyContent2(){
    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
            
          // Creating a Text  
          Text("Hello Geek!", fontSize = 50.sp)
    }
}
  
// For displaying preview in
// the Android Studio IDE emulator
@Preview(showBackground = true)
@Composable
fun DefaultPreview2() {
    MainContent2()
}


Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcintentnextactivity
  
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // Calling the composable function
            // to display element and its contents
            MainContent()
        }
    }
}
  
// Creating a composable 
// function to display Top Bar
@Composable
fun MainContent() {
    Scaffold(
        topBar = { TopAppBar(title = { Text("GFG | Main Activity", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
        content = { MyContent() }
    )
}
  
// Creating a composable function to
// create two Images and a spacer between them
// Calling this function as content in the above function
@Composable
fun MyContent(){
  
    // Fetching the Local Context
    val mContext = LocalContext.current
  
    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
         
          // Creating a Button that on-click 
          // implements an Intent to go to SecondActivity
          Button(onClick = {
            mContext.startActivity(Intent(mContext, SecondActivity::class.java))
        },
            colors = ButtonDefaults.buttonColors(backgroundColor = Color(0XFF0F9D58)),
        ) {
            Text("Go to Second Activity", color = Color.White)
        }
    }
}
  
// For displaying preview in
// the Android Studio IDE emulator
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MainContent()
}


Output:

When you run the application, you will see a button with the text ‘Go to Second Activity. When you click it, the application will take you to SecondActivity which displays the text ‘Hello Geek!’. Below is the recording of the application.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads