Android Application
Kotlin 인텐트(intent) : 암시적 인텐트 (Implicit Intent)-Data 전달하기
작성자 임베디드코리아
작성일 23-10-23 22:55
조회1,458회
댓글0건
Kotlin 인텐트(intent) : 암시적 인텐트 (Implicit Intent)-Data 전달하기
메인 Activity에서 Second 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">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="20dp"
android:hint="텍스트를 입력하세요"
app:layout_constraintBottom_toTopOf="@+id/GoSecondActivityBTN"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/GoSecondActivityBTN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="400dp"
android:text="Go SecondActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
-------------------------------------------------------------------------------------
***** activity_second.xml **
--- res/layout 에서 ---> New --> Layout Resource File
--- File : activity_sub , Source set : main
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/reciveData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
<Button
android:id="@+id/ReturnBTN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="돌아가기" />
</LinearLayout>
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var SecondActivityBTN: Button
private lateinit var messageET: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title ="암시적 인텐트 호출"
SecondActivityBTN = findViewById(R.id.GoSecondActivityBTN)
messageET = findViewById(R.id.editText)
SecondActivityBTN.setOnClickListener {
val intent = Intent( "intent.implicit.second")
intent.putExtra("KEY",messageET.text.toString())
//intent.putExtra("KEY", "쫓기는 짐승이 되지 말고, 싸우는 영웅이 되자 !")
startActivity(intent)
}
messageET.setOnClickListener {
messageET.setText(null)
}
}
}
------------------------------------------------------------------------------------
***** SecondActivity.kt **
-- New --> Kotlin Class/File
-- Name : SecondActivity
------------------------------------------------------------------------------------
class SecondActivity: AppCompatActivity() {
private lateinit var btnReturn: Button
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
title = "암시적 Second 액티비티"
btnReturn = findViewById<Button>(R.id.ReturnBTN)
textView = findViewById<TextView>(R.id.reciveData)
var ReciveMSG = this.intent.getStringExtra("KEY") //전송부의 키값의 value를 가져온다.
Toast.makeText(applicationContext, "메인로 부터 데이터를 받았습니다.", Toast.LENGTH_LONG).show()
textView.text="$ReciveMSG"
btnReturn.setOnClickListener {
//setResult(2900)
finish()
}
}
}
------------------------------------------------------------------------------------
themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.ImplicitIntentAtivityData" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#2196F3</item>
<item name="colorPrimaryDark">#FFC107</item>
</style>
<style name="Theme.ImplicitIntentAtivityData" parent="Base.Theme.ImplicitIntentAtivityData" />
</resources>
------------------------------------------------------------------------------------
AndroidManifest.xml
------------------------------------------------------------------------------------
추가하기
<activity
android:name=".SecondActivity"
android:exported="true" >
<intent-filter>
<action android:name="intent.implicit.second" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.