An Intent is how you navigate from one Activity to another and optionally pass data along with it:
// In MainActivity
binding.viewProfileButton.setOnClickListener {
val intent = Intent(this, ProfileActivity::class.java)
intent.putExtra("USER_NAME", "Ada Lovelace")
startActivity(intent)
}
// In ProfileActivity
class ProfileActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityProfileBinding.inflate(layoutInflater)
setContentView(binding.root)
val userName = intent.getStringExtra("USER_NAME") ?: "Guest"
binding.profileNameText.text = userName
}
}
Note the ?: "Guest" — getStringExtra always returns a nullable String?, since the calling Activity might not have supplied that extra at all. Handling that possibility explicitly, rather than assuming the extra will always be there, avoids a very common source of crashes.
Every Activity you create must also be declared in AndroidManifest.xml:
<activity android:name=".ProfileActivity" />
- Image
- SKU
- Rating
- Price
- Stock
- Availability
- Add to cart
- Description
- Content
- Weight
- Dimensions
- Additional information