In this post I will show you how you can pass data between activities in Android Compose using intents.
I show two methods below which are both built into the same sample activities.
One method just sends data to a new activity that can be used for further processing. This can be useful for passing a username, user id, etc.
The second method is used to call an activity to open, captures a result from it and sends it back to the previous activity. My most recent use for this method was to open an activity that accesses a Room database to get a selected value. The activity allows the creation, deletion and editing of database entries. When the user is satisfied with their choice, they can click it to pass back to the previous activity for further processing.
Code
This is Activity A ( MainActivity )
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
var input by remember { mutableStateOf("") }
var getResultString by remember { mutableStateOf("") }
val context = LocalContext.current
val getInput = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()){result ->
if(result.resultCode == Activity.RESULT_OK){
getResultString = result.data?.getStringExtra("passData") ?: ""
}
}
SamplePassingIntentDataTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Box(modifier = Modifier.fillMaxSize().padding(innerPadding), contentAlignment = Alignment.Center){
Column {
Text(text = "Activity A\n\nData from Activity B: \n$getResultString")
TextField(value = input, onValueChange = { input = it })
Button(onClick = {
startActivity(Intent(context, ActivityB::class.java).apply{ putExtra("input", input) })
}) { Text(text = "Pass input to Activity B") }
Button(onClick = {
getInput.launch(Intent(context, ActivityB::class.java).apply{ putExtra("getResult", true) })
}) { Text(text = "Get input from Activity B") }
}
}
}
}
}
}
}
This is the Activity B
class ActivityB : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
val fromIntent = intent.getStringExtra("input") ?: ""
val getResult = intent.getBooleanExtra("getResult", false)
var input by remember { mutableStateOf("") }
val context = LocalContext.current
SamplePassingIntentDataTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
contentAlignment = Alignment.Center
) {
Column {
Text(text = "Activity B\n\nData from Activity A: \n$fromIntent")
if(getResult) {
TextField(value = input, onValueChange = { input = it })
Button(onClick = {
val data = Intent().apply {
putExtra("passData", input)
}
setResult(RESULT_OK, data)
finish()
}) { Text(text = "Pass input to Activity B") }
}
}
}
}
}
}
}
}
Bonus
Below is a code snippet of how to share data, in this case a string, to any application that accepts the type specified using the intent action method.
val sendIntent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, strBuilder.toString())
}
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
Hope this was useful.