0
0
Android Kotlinmobile~20 mins

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

Choose your learning style9 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