μΆμ²: https://sesac.seoul.kr/ (JetPackκ³Ό Kotlinμ νμ©ν Android App κ°λ° κ°μ)
MainActivityμ κ°μ λλ ν 리μ Empty Activityλ₯Ό μΆκ°νλ©΄, AndroidManifest.xml
νμΌμ μλμΌλ‘ μλ‘μ΄ μ‘ν°λΉν°κ° λ±λ‘λλ€. μμ€ν
μμ μλͺ
μ£ΌκΈ°κ° κ΄λ¦¬λλ μ»΄ν¬λνΈλ€μ AndroidManifest.xml
νμΌμ κΌ λ±λ‘ν΄μ€μΌ νλ€.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.c40" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLab" >
<activity
android:name=".DetailActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.tutorial.c40
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 button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val intent = Intent(this, DetailActivity::class.java)
startActivity(intent)
}
}
}
// μΈν
νΈμ λ°μ΄ν° μ λ¬
public Intent putExtra(String name, CharSequence value)
// μΈν
νΈλ₯Ό ν΅ν΄ λ°μ΄ν° νλ
public int getIntExtra(String name, int defaultValue)
public String getStringExtra(String name)
public double getDoubleExtra(String name, double defaultValue)
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</ListView>
package com.tutorial.c40
import android.content.Intent
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listView = findViewById<ListView>(R.id.main_list)
val data = arrayOf("android", "kotlin", "jetpack")
val adapter = ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
data
)
listView.adapter = adapter
listView.setOnItemClickListener { adapterView, view, i, l ->
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("id", i)
intent.putExtra("title", data[i])
startActivity(intent)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".DetailActivity">
<TextView
android:id="@+id/resultView"
android:textSize="30sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.tutorial.c40
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class DetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val id = intent.getIntExtra("id", 0)
val title = intent.getStringExtra("title")
val resultView = findViewById<TextView>(R.id.resultView)
resultView.text = "id: $id, title: $title"
}
}
μΈν νΈλ‘ μ‘ν°λΉν°λ₯Ό μ€νμν€κ³ κ²°κ³Όλ₯Ό λλ € λ°λ λ°©λ²μ λκ°μ§κ° μλ€.
public void startActivityForResult(Intent intent, int requestCode)
val intent = Intent(this, DetailActivity::class.java)
startActivityForResult(intent, 10)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == 10 && resultCode == RESULT_OK) {
// ...
}
}
val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
// ...
}
val intent = Intent(this, DetailActivity::class.java)
resultLauncher.launch(intent)
intent.putExtra("result", "hello $id")
setResult(RESULT_OK, intent)
finish()
MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/resultView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="startActivityForResult()" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="ActivityResultLauncher" />
</LinearLayout>
package com.tutorial.c41
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var resultView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn1 = findViewById<Button>(R.id.btn1)
val btn2 = findViewById<Button>(R.id.btn2)
resultView = findViewById(R.id.resultView)
// λ°©λ² 1) startActivityForResult() ν¨μ
btn1.setOnClickListener {
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("id", "first") // λΆκ° λ°μ΄ν° μ λ¬
startActivityForResult(intent, 10)
}
// λ°©λ² 2) registerForActivityResult() ν¨μ
val resultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()){
resultView.text = "result: ${it.data?.getStringExtra("result")}"
}
btn2.setOnClickListener {
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("id", "second") // λΆκ° λ°μ΄ν° μ λ¬
resultLauncher.launch(intent)
}
}
// λ°©λ² 1μ λν μ²λ¦¬
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == 10 && resultCode == RESULT_OK){
val result: String? = data?.getStringExtra("result") // λΆκ° λ°μ΄ν° λ°κΈ°
resultView.text = "result : $result"
}
}
}
DetailActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".DetailActivity">
<Button
android:id="@+id/finishButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="finish"/>
</LinearLayout>
package com.tutorial.c41
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class DetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val id = intent.getStringExtra("id") // λΆκ° λ°μ΄ν° λ°κΈ°
val button = findViewById<Button>(R.id.finishButton)
// λ°©λ² 2μ λν μ²λ¦¬
button.setOnClickListener {
intent.putExtra("result", "hello $id")
setResult(RESULT_OK, intent)
finish()
}
}
}
μΈν νΈμ μν΄ μ€νλλ "μ»΄ν¬λνΈμ μ 보"λ‘ μ΄λ€ κ²μ μ΄μ©νλμ λ°λΌ μλ λκ°μ§λ‘ ꡬλΆλ¨.
- λͺ μμ μΈν νΈ: ν΄λμ€ νμ μ°Έμ‘° μ 보λ₯Ό νμ©ν μΈν νΈ
- μμμ μΈν νΈ: μΈν νΈ νν° μ 보λ₯Ό νμ©ν μΈν νΈ
λͺ μμ μΈν νΈμλ ν΄λμ€λͺ μ 보λ₯Ό λ΄μμ μμ€ν μ μΈν νΈλ₯Ό λ°μμν¨λ€. κ·Έλ¬λ©΄ μμ€ν μ μΈν νΈμ λ΄κΈ΄ ν΄λμ€λͺ μ 보λ₯Ό λ³΄κ³ ν΄λΉ μ»΄ν¬λνΈλ₯Ό μ€νμν¨λ€. μ΄λ¬ν κ΅¬μ‘°κ° κ°λ₯νλ €λ©΄, μΈν νΈμ μ€ν λμμ΄ λλ μ»΄ν¬λνΈκ° μλλ‘μ΄λ λ©μΈ νκ²½ νμΌμΈ 맀λνμ€νΈ νμΌμ λ±λ‘λμ΄ μμ΄μΌ νλ€. activity νκ·Έμ νμ μμ±μΈ nameμ ν΄λμ€λͺ μ 보λ₯Ό μμ±νλ©΄ λλ€.
νλ‘κ·Έλ¨μ΄ λμνλ€κ° μ΄λ μκ°μ μΈν νΈκ° λ°μνλ©΄, ν΄λΉ μΈν νΈ μ 보μ μμ€ν μ΄ λ§€λνμ€νΈ νμΌμμ μΈμ§ν μ»΄ν¬λνΈ μ 보λ₯Ό μλ‘ λΉκ΅νμ¬ μ΄ν리μΌμ΄μ μ μ€νμν¨λ€.
μ΄μ²λΌ, 맀λνμ€νΈ νμΌμ "ν΄λμ€λͺ μ 보λ§" λ±λ‘νλ©΄ ν΄λΉ μ»΄ν¬λνΈλ λͺ μμ μΈν νΈμ μν΄μλ§ μ€νλλ€. κ·Έλ¦¬κ³ λͺ μμ μΈν νΈλ νλμ μ΄ν리μΌμ΄μ λ΄μ μλ μλ‘ λ€λ₯Έ μ»΄ν¬λνΈ κ°μλ μ΄μ© κ°λ₯νμ§λ§, μΈλΆ μ±μ μλ μ»΄ν¬λνΈμμλ μ΄μ© λΆκ°λ₯νλ€.
μμμ μΈν νΈλ μΈν νΈ νν° μ 보λ₯Ό μ΄μ©νλ€. μ¦, 맀λνμ€νΈ νμΌμ μ»΄ν¬λνΈ μ 보λ₯Ό λ±λ‘ν λ ν΄λμ€λͺ λΏλ§ μλλΌ μΈν νΈ νν°μ λ€λ₯Έ μ 보λ λ±λ‘ν΄μ€μΌ μμμ μΈν νΈλ‘ ν΄λΉ μ»΄ν¬λνΈλ₯Ό μ€νμν¬ μ μλ€. μ΄μ²λΌ μΈν νΈ νν° μ 보λ₯Ό μ΄μ©νλ©΄, λ€λ₯Έ μ΄ν리μΌμ΄μ μ ν΄λμ€λͺ μ 보λ₯Ό λͺ¨λ₯΄λλΌλ μμμ μΈν νΈλ₯Ό ν΅ν΄ μΈλΆ μ±μ μ»΄ν¬λνΈλ μ€νμν¬ μ μλ€.
μ°λ¦¬κ° μλλ‘μ΄λ μ€νλμ€μμ μ±μ λ§λ€λ©΄ 맀λνμ€νΈ νμΌμ μλμΌλ‘ λ€μκ³Ό κ°μ λ΄μ©μ΄ λ±λ‘λλ€. μ¦, μ°λ¦¬μ μ±λ μΈλΆ μ±μΈ λ°μ² μ±μμ μμ΄μ½μ ν΄λ¦νλ©΄ μΈν νΈκ° λ°μνμ¬ μ€νμ΄ λλ μ리μ΄κΈ° λλ¬Έμ, μΈν νΈ νν°μ μλμ κ°μ λ΄μ©μ΄ λ±λ‘λμ΄ μλ κ²μ΄λ€.
κ²°κ΅ μμ μ½λμμ μμμ μΈν νΈμλ ν΄λμ€λͺ μ λ³΄κ° μμ§λ§, μΈν νΈ νν°μ μν΄ μμ€ν μ λ±λ‘λμ΄ μλ μ»΄ν¬λνΈ μ€μμ μ‘μ λ¬Έμμ΄μ΄ "ACTION_EDIT"μ΄κ³ λ°μ΄ν° μ λ³΄κ° "http"λ‘ λμ΄ μλ μ»΄ν¬λνΈκ° μ€νλλ€.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.c42">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLab">
<activity
android:name=".DetailActivity"
android:exported="false">
<intent-filter>
<action android:name="ACTION_DETAIL"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity
<?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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DetailActivity"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.tutorial.c42
import android.content.Intent
import android.net.Uri
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 button = findViewById<Button>(R.id.button)
button.setOnClickListener {
// μ¬κΈ°μ μ§μ ν μΈν
νΈ νν° μ 보μ μΌμΉνλ μ‘ν°λΉν°λ₯Ό μ€ννλ€. (μμμ μΈν
νΈ)
val intent = Intent()
intent.action = "ACTION_DETAIL"
intent.data = Uri.parse("http://www.google.com")
startActivity(intent)
}
}
}
DetailActivity
<?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=".DetailActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am DetailActivity"
android:textStyle="bold"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.tutorial.c42
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class DetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
}
}