Android Application
Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent) - 기본 인텐트
작성자 임베디드코리아
작성일23-10-23 22:47
조회1,482회
댓글0건
Kotlin 인텐트(intent) : 명시적 인텐트(Explicit Intent)
- 기본적인 인텐트로 버튼을 클릭하면 Sub Activity 를 수행하고, 버튼을 클릭하면 다시 뒤돌온다.
-------------------------------------------------------------------------------------
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnGoSubActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to SubActivity"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
-------------------------------------------------------------------------------------
***** activity_sub.xml **
--- res/layout 에서 ---> New --> Layout Resource File
--- File : activity_sub , Source set : 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"
android:background="#E1DCB0"
tools:context=".SubActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 여기는 \n Sub Activity \n 입니다."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var btnGoSubActivity: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnGoSubActivity = findViewById(R.id.btnGoSubActivity)
btnGoSubActivity.setOnClickListener {
val intent = Intent(this, SubActivity::class.java)
startActivity(intent)
}
}
}
------------------------------------------------------------------------------------
***** SubActivity.kt **
-- New --> Kotlin Class/File
-- Name : SubActivity
------------------------------------------------------------------------------------
class SubActivity : AppCompatActivity() {
private lateinit var btnClose: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sub)
btnClose = findViewById(R.id.btnClose)
btnClose.setOnClickListener {
finish()
}
}
}
------------------------------------------------------------------------------------
AndroidManifest.xml
------------------------------------------------------------------------------------
추가하기
<activity
android:name=".SubActivity" >
</activity>