728x90
이번 시간에는 activity를 여러개 쌓은 후, 마지막 것을 기준으로 한번에 종료하는 기능을 만들어보겠습니다.
해당 기능을 구현하는 방법은 여러가지가 있습니다.
하지만 그것들 중에서 ActivityResultLauncher<Intent> 를 사용할 것 입니다.
원리는 간단합니다. launcher을 통해 activity를 실행시킨후, 던진 데이터가 finish이면 종료시킵니다.
이를 위해선 매 activity launcher가 필요하므로 BaseLauncherActivity를 만들어 상속해줍시다.
open class BaseLauncherActivity : AppCompatActivity() {
protected lateinit var launcher: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
launcher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
if (result.resultCode == RESULT_OK) {
val data = result.data
setResult(RESULT_OK, data)
finish()
}
}
}
}
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var launcher: ActivityResultLauncher<Intent>
private lateinit var tv : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv = findViewById(R.id.result)
findViewById<Button>(R.id.button2).setOnClickListener {
val intent = Intent(baseContext, Signup1Activity::class.java)
launcher.launch(intent)
}
launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
if (result.resultCode == RESULT_OK) {
val data = result.data
tv.text = "id : ${data?.getStringExtra("id")}\npassword : ${data?.getStringExtra("id")}\n소개말 : ${data?.getStringExtra("description")}"
}
}
}
}
class Signup1Activity : BaseLauncherActivity() {
private lateinit var et : EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.signup1_activity)
et = findViewById(R.id.editText)
findViewById<Button>(R.id.button).setOnClickListener {
val intent = Intent(baseContext, Signup2Activity::class.java)
intent.putExtra("id", et.text.toString())
launcher.launch(intent)
}
}
}
class Signup2Activity : BaseLauncherActivity() {
private lateinit var et : EditText
private lateinit var id : String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.signup2_activity)
if (intent.hasExtra("id")) {
id = intent.getStringExtra("id")!!
} else {
finish()
}
et = findViewById(R.id.editText)
findViewById<Button>(R.id.button).setOnClickListener {
val intent = Intent(baseContext, Signup3Activity::class.java)
intent.putExtra("id", id)
intent.putExtra("password", et.text.toString())
launcher.launch(intent)
}
}
}
class Signup3Activity : BaseLauncherActivity() {
private lateinit var et : EditText
private lateinit var id : String
private lateinit var password : String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.signup3_activity)
et = findViewById(R.id.editText)
if (intent.hasExtra("id") && intent.hasExtra("password")) {
id = intent.getStringExtra("id")!!
password = intent.getStringExtra("password")!!
} else {
finish()
}
findViewById<Button>(R.id.button).setOnClickListener {
val data = Intent()
data.putExtra("id", id)
data.putExtra("password", password)
data.putExtra("description", et.text.toString())
data.putExtra("finish", true)
setResult(RESULT_OK, data)
super.finish()
}
}
}
activity_main
<?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">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="가입해주세요."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="가입"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/result" />
</androidx.constraintlayout.widget.ConstraintLayout>
signup1_activity.xml
<!-- signup1_activity -->
<?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=".Signup1Activity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="68dp"
android:text="다음"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="60dp"
android:text="아이디"
app:layout_constraintEnd_toStartOf="@+id/editText"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="30dp"
android:hint="아이디"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
signup2_activity
<?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=".Signup1Activity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="68dp"
android:text="다음"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="60dp"
android:text="비밀번호"
app:layout_constraintEnd_toStartOf="@+id/editText"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="30dp"
android:inputType="textPassword"
android:hint="비밀번호"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
signup3_activity
<?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=".Signup1Activity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="68dp"
android:text="완료"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:text="자기 소개"
app:layout_constraintBottom_toBottomOf="@+id/editText"
app:layout_constraintEnd_toStartOf="@+id/editText"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/editText" />
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="30dp"
android:hint="자기소개"
android:inputType="text|textMultiLine"
android:lines="20"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
>> 결과물
=
'안드로이드 코틀린' 카테고리의 다른 글
[Kotlin][Android] 파일 쓰고 불러오기 (0) | 2022.07.13 |
---|---|
[kotlin][Android] 안드로이드에서 긴 글 가져오기 (0) | 2022.07.07 |
[Kotlin][Android] BottomSheetDialogFragment 구현해보기 (0) | 2022.06.22 |
[Kotlin][Android] 디바이스 전체 화면 길이 구하기 (0) | 2022.06.18 |
[Kotlin][Android] Lambda 사용해서 계산 기능 만들기 (0) | 2022.06.13 |