Intent
- An Intent is a messaging object you can use to request an action from another app component.
- Three fundamental use cases:
- Two types of intents:
- Explicit intents: Specify package name or component class name
- Implicit intents: Do not specify above name
Explicit Intent
- Specify package name or component class name
- Start an activity in Intent object
- Data can be passed via Extras
Activity Lifecycle
- startActivity
- Pressing home button
- Get back to application
- Back button(close)
Practice
- finish() to kill activity
- Text clear on on[Some function on activity lifecycle]() function
- edittext.text.clear() when activity re-loaded
MainActivity.kt
package com.example.week6
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
class MainActivity : AppCompatActivity() {
companion object{
const val EXT_NAME = "extra_key_name"
const val EXT_AGE = "extra_key_age"
}
override fun onResume() {
super.onResume()
val editTextName = findViewById<EditText>(R.id.editTextName)
val editTextAge = findViewById<EditText>(R.id.editTextAge)
editTextName.text.clear()
editTextAge.text.clear()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) //override 할 때 super call 해줘야 함
setContentView(R.layout.activity_main)
val btnNewActivity = findViewById<Button>(R.id.buttonNewActivity)
btnNewActivity.setOnClickListener {
val editTextName = findViewById<EditText>(R.id.editTextName)
val editTextAge = findViewById<EditText>(R.id.editTextAge)
val name = editTextName.text.toString()
val age = editTextAge.text.toString().toInt()
val intent = Intent(this, NothingActivity::class.java).apply {
putExtra(EXT_NAME, name)
putExtra(EXT_AGE, age)
}
startActivity(intent)
}
}
}
NothingActivity.kt
package com.example.week6
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class NothingActivity : AppCompatActivity() {
companion object{
const val EXT_NAME = "extra_key_name"
const val EXT_AGE = "extra_key_age"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nothing)
val name = intent.getStringExtra(MainActivity.EXT_NAME)
val age = intent.getIntExtra(MainActivity.EXT_AGE, -1)
val textView = findViewById<TextView>(R.id.textView3)
textView.text = "You typed name ${name} and age ${age}, is that right?"
val yesButton = findViewById<Button>(R.id.button)
yesButton.setOnClickListener{
val intent = Intent(this, LastActivity::class.java).apply {
putExtra(MainActivity.EXT_NAME, name)
putExtra(MainActivity.EXT_AGE, age)
}
startActivity(intent)
}
val noButton = findViewById<Button>(R.id.button2)
noButton.setOnClickListener {
finish()
}
}
}
LastActivity.kt
package com.example.week6
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
class LastActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_last)
val name = intent.getStringExtra(NothingActivity.EXT_NAME)
val sid = intent.getIntExtra(NothingActivity.EXT_AGE, -1)
Toast.makeText(
applicationContext,
"Welcome, ${name}(${sid})!",
Toast.LENGTH_SHORT
).show()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonNewActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/editTextName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Enter your name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="Enter your age"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/editTextName" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.2" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Name"
app:layout_constraintBottom_toBottomOf="@+id/editTextName"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/editTextName" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Age"
app:layout_constraintBottom_toBottomOf="@+id/editTextAge"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/editTextAge" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_nothing.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NothingActivity">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="YES"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline4"
app:layout_constraintVertical_bias="0.54" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="NO"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.8" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_last.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LastActivity">
</androidx.constraintlayout.widget.ConstraintLayout>