Bird
Raised Fist0
Android Kotlinmobile~20 mins

Intent for activity navigation in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: MainActivity and SecondActivity
This app has two screens. The first screen has a button. When you tap the button, it opens the second screen.
Target UI
MainActivity Screen:
+-----------------------+
|                       |
|    [Go to Second]     |
|                       |
+-----------------------+

SecondActivity Screen:
+-----------------------+
|                       |
|   Welcome to Second   |
|        Screen         |
|                       |
+-----------------------+
MainActivity with a Button labeled 'Go to Second'
When the button is tapped, navigate to SecondActivity using Intent
SecondActivity shows a TextView with text 'Welcome to Second Screen'
Both activities must be declared in AndroidManifest.xml
Starter Code
Android Kotlin
package com.example.intentnavigation

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val buttonGoSecond = findViewById<Button>(R.id.buttonGoSecond)
        // TODO: Add button click listener to start SecondActivity
    }
}

// TODO: Create SecondActivity class with a TextView
Task 1
Task 2
Task 3
Solution
Android Kotlin
package com.example.intentnavigation

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val buttonGoSecond = findViewById<Button>(R.id.buttonGoSecond)
        buttonGoSecond.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
    }
}

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val textView = TextView(this).apply {
            text = "Welcome to Second Screen"
            textSize = 24f
            setPadding(50, 50, 50, 50)
        }
        setContentView(textView)
    }
}

/* AndroidManifest.xml snippet:
<application ... >
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecondActivity" />
</application>
*/

We added a click listener to the button in MainActivity. When clicked, it creates an Intent to start SecondActivity and calls startActivity to navigate.

SecondActivity is a simple screen that shows a TextView with a welcome message. We set this view directly in code for simplicity.

Both activities are declared in AndroidManifest.xml. This is required so Android knows about them.

Final Result
Completed Screen
MainActivity Screen:
+-----------------------+
|                       |
|    [Go to Second]     |
|                       |
+-----------------------+

SecondActivity Screen:
+-----------------------+
|                       |
|   Welcome to Second   |
|        Screen         |
|                       |
+-----------------------+
User taps 'Go to Second' button on MainActivity
App opens SecondActivity showing 'Welcome to Second Screen' text
Stretch Goal
Add a Back button on SecondActivity to return to MainActivity
💡 Hint
Use the default ActionBar back button by enabling it in SecondActivity with supportActionBar?.setDisplayHomeAsUpEnabled(true) and handle onOptionsItemSelected

Practice

(1/5)
1. What is the main purpose of an Intent in Android Kotlin?
easy
A. To store user data permanently
B. To style the user interface
C. To navigate from one activity to another
D. To handle background tasks

Solution

  1. Step 1: Understand Intent usage

    An Intent is used to request an action from another app component, commonly to start a new activity.
  2. Step 2: Identify navigation role

    In Android, Intent is primarily used to navigate between screens (activities).
  3. Final Answer:

    To navigate from one activity to another -> Option C
  4. Quick Check:

    Intent = Navigation [OK]
Hint: Intent is for moving between screens [OK]
Common Mistakes:
  • Confusing Intent with data storage
  • Thinking Intent styles UI
  • Assuming Intent handles background tasks
2. Which of the following is the correct syntax to create an Intent to start SecondActivity from the current activity?
easy
A. val intent = Intent(this, SecondActivity::class.java)
B. val intent = Intent(SecondActivity, this::class.java)
C. val intent = Intent(this, SecondActivity)
D. val intent = Intent(SecondActivity::class.java, this)

Solution

  1. Step 1: Check Intent constructor parameters

    The Intent constructor requires the current context and the target activity class with ::class.java.
  2. Step 2: Validate syntax correctness

    val intent = Intent(this, SecondActivity::class.java) correctly uses this for context and SecondActivity::class.java for the target.
  3. Final Answer:

    val intent = Intent(this, SecondActivity::class.java) -> Option A
  4. Quick Check:

    Intent(this, TargetActivity::class.java) = Correct [OK]
Hint: Use this and TargetActivity::class.java in Intent [OK]
Common Mistakes:
  • Swapping parameters order
  • Omitting ::class.java
  • Using activity instance instead of class
3. What will happen when the following code runs inside an activity?
val intent = Intent(this, DetailActivity::class.java)
startActivity(intent)
medium
A. The app will crash due to missing permissions
B. The current activity will close and DetailActivity will open
C. Nothing will happen because startActivity is missing parameters
D. DetailActivity will open on top of the current activity

Solution

  1. Step 1: Analyze Intent creation

    The Intent is correctly created to open DetailActivity from the current context.
  2. Step 2: Understand startActivity behavior

    Calling startActivity launches the new activity on top without closing the current one.
  3. Final Answer:

    DetailActivity will open on top of the current activity -> Option D
  4. Quick Check:

    startActivity(intent) = Opens new screen on top [OK]
Hint: startActivity opens new screen on top [OK]
Common Mistakes:
  • Thinking startActivity closes current activity
  • Assuming permissions needed for navigation
  • Believing startActivity requires extra parameters
4. Identify the error in this code snippet that tries to start ProfileActivity:
val intent = Intent(ProfileActivity::class.java, this)
startActivity(intent)
medium
A. The parameters in Intent are reversed
B. startActivity is missing parentheses
C. ProfileActivity should be an instance, not a class
D. Intent requires a string action, not classes

Solution

  1. Step 1: Check Intent constructor parameter order

    The Intent constructor expects the current context first, then the target activity class.
  2. Step 2: Identify parameter reversal

    Here, the target activity class is given first, then the context, which is incorrect.
  3. Final Answer:

    The parameters in Intent are reversed -> Option A
  4. Quick Check:

    Intent(this, TargetActivity::class.java) = Correct order [OK]
Hint: Context first, target class second in Intent [OK]
Common Mistakes:
  • Swapping context and target activity
  • Forgetting ::class.java
  • Using activity instance instead of context
5. You want to open SettingsActivity and close the current activity so the user cannot return by pressing back. Which code snippet achieves this?
hard
A. val intent = Intent(this, SettingsActivity::class.java) startActivity(intent)
B. val intent = Intent(this, SettingsActivity::class.java) startActivity(intent) finish()
C. val intent = Intent(this, SettingsActivity::class.java) finish() startActivity(intent)
D. val intent = Intent(this, SettingsActivity::class.java) startActivityForResult(intent, 1)

Solution

  1. Step 1: Understand startActivity and finish

    startActivity opens the new activity; finish closes the current one.
  2. Step 2: Order matters

    Calling finish() after startActivity ensures the new screen opens before current closes.
  3. Step 3: Check options

    val intent = Intent(this, SettingsActivity::class.java) startActivity(intent) finish() correctly calls startActivity then finish to prevent back navigation.
  4. Final Answer:

    val intent = Intent(this, SettingsActivity::class.java) startActivity(intent) finish() -> Option B
  5. Quick Check:

    startActivity + finish = Open new and close current [OK]
Hint: Call finish() after startActivity to close current [OK]
Common Mistakes:
  • Calling finish() before startActivity
  • Not calling finish() at all
  • Using startActivityForResult unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard