Sobes.tech
Junior

How is navigation between Activities in Android implemented?

sobes.tech AI

Answer from AI

Navigation between Activities is performed using Intents.

An Intent is a message object that allows components (such as Activities, Services, Broadcast Receivers) to request actions from each other.

There are two main types of Intents:

  • Explicit Intents: Specify a particular component to start (e.g., an Activity class).
  • Implicit Intents: Specify the type of action to perform (e.g., view a web page), and the Android system chooses an appropriate component to handle it.

To start a new Activity with an explicit Intent, the startActivity() method is used.

// In the current Activity
val intent = Intent(this, TargetActivity::class.java) // Explicit Intent
startActivity(intent)

To pass data between Activities, the putExtra() method of the Intent object can be used:

// Sending data
val intent = Intent(this, TargetActivity::class.java)
intent.putExtra("key_name", "some_value")
startActivity(intent)

// Receiving data in TargetActivity (in onCreate() or onNewIntent())
val data = intent.getStringExtra("key_name")

To get a result from a launched Activity, rather than just starting a new one, startActivityForResult() is used. After the child Activity finishes, it can send back a result using setResult() and finish(). The parent Activity receives this result in the onActivityResult() method.

// In the parent Activity
val intent = Intent(this, ChildActivity::class.java)
startActivityForResult(intent, REQUEST_CODE) // REQUEST_CODE is a unique identifier

// Receiving the result in the parent Activity
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
        val resultData = data?.getStringExtra("result_key")
        // Handle the result
    }
}

// In the child Activity (before closing)
val resultIntent = Intent()
resultIntent.putExtra("result_key", "result_value")
setResult(RESULT_OK, resultIntent)
finish()

Since AndroidX, it is recommended to use the Activity Results API instead of startActivityForResult()/onActivityResult():

// In the parent Activity
private val startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        val data = result.data
        // Handle the result
    }
}

// Launching the Activity
val intent = Intent(this, ChildActivity::class.java)
startForResult.launch(intent)

// In the child Activity (before closing)
val resultIntent = Intent()
resultIntent.putExtra("result_key", "result_value")
setResult(RESULT_OK, resultIntent)
finish()

For more complex navigation in modern Android applications, the Navigation component from Jetpack is often used:

  • Navigation Component: Allows defining a navigation graph that describes all possible paths between app screens. Uses NavController to manage navigation.
<!-- Example nav_graph.xml -->
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.example.app.FirstFragment"
        android:label="First" >
        <action
            android:id="@+id/action_firstFragment_to_secondFragment"
            app:destination="@id/secondFragment" />
    </fragment>
    <fragment
        android:id="@+id/secondFragment"
        android:name="com.example.app.SecondFragment"
        android:label="Second" />
</navigation>
// In an Activity (e.g., in a Fragment)
findNavController().navigate(R.id.action_firstFragment_to_secondFragment)

The Navigation Component simplifies navigation implementation, including argument passing, dialog support, and back stack management.

How is navigation between Activities in Android… - sobes.tech