Learn & Record
Android (양방향 액티비티, 암시적 인텐트, 액티비티와 인텐트) 본문
1. 양방향 액티비티
- 메인 액티비에서 세컨드로 넘긴 후
- 세컨드에서 메인트로 데이터를 돌려주는 경우
<?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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="더하기"/>
</LinearLayout>
- 메인 액티비티 XML
- 액티비티추가 : New > Activity > Empty Activity 클릭
- 위 방식으로 추가하면 mainfests, xml 자동 적용
<?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"
tools:context=".SecondActivity">
<Button
android:id="@+id/btnReturn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"/>
</LinearLayout>
- SecondActivity XML 작성
package kr.jeongmo.a0508_project_09_small_02
import android.app.Activity
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() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "메인 액티비티"
val editNum1 = findViewById<EditText>(R.id.editNum1)
val editNum2 = findViewById<EditText>(R.id.editNum2)
val btnNewActivity = findViewById<Button>(R.id.btnNewActivity)
btnNewActivity.setOnClickListener {
val intent = Intent(applicationContext, SecondActivity::class.java)
intent.putExtra("num1", editNum1.text.toString().toInt())
intent.putExtra("num2", editNum1.text.toString().toInt())
startActivityForResult(intent, 0)
}
}
override fun onActivityResult(requestCode: Int, resultCode:Int, data:Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
val sum = data!!.getIntExtra("sum", 0)
Toast.makeText(applicationContext, "합계 : ${sum}", Toast.LENGTH_SHORT).show()
}
}
}
- 메인 액티비티
package kr.jeongmo.a0508_project_09_small_02
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
title = "Second 액티비티"
// 인텐트 관련 처리
val intent = intent
val sum = intent.getIntExtra("num1", 0) + intent.getIntExtra("num2", 0)
val btnReturn = findViewById<Button>(R.id.btnReturn)
btnReturn.setOnClickListener {
val intentResult = Intent(applicationContext, MainActivity::class.java)
intentResult.putExtra("sum", sum)
setResult(Activity.RESULT_OK, intentResult)
finish()
}
}
}
- 세컨드 액티비티
2. 암시적 인텐트
- 명시적 인텐트의 개념이 두 액티비티를 사용자가 직접 생성하고 코딩하는 것이라면,
- 암시적 인텐트는 약속된 액션을 지정하여 '안드로이드에서 제공하는 기존 응용 프로그램을 실행하는 것',
- 예를 들어 전화번호를 인텐트로 넘긴 후에 전화 걸기 응용 프로그램이 실행되는 것.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.jeongmo.a0508_intent_02">
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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.0508_intent_02">
<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>
- manifest
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화 걸기"/>
<Button
android:id="@+id/btnWeb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="홈 페이지 열기"/>
<Button
android:id="@+id/btnGoogle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="구글 맵 열기"/>
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="구글 검색하기"/>
<Button
android:id="@+id/btnSms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="문자 보내기"/>
<Button
android:id="@+id/btnPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="사진 찍기"/>
</LinearLayout>
- xml
package kr.jeongmo.a0508_intent_02
import android.app.SearchManager
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "암시적 인텐트 예제"
val btnCall = findViewById<Button>(R.id.btnCall)
val btnWeb = findViewById<Button>(R.id.btnWeb)
val btnGoogle = findViewById<Button>(R.id.btnGoogle)
val btnSearch = findViewById<Button>(R.id.btnSearch)
val btnSms = findViewById<Button>(R.id.btnSms)
val btnPhoto = findViewById<Button>(R.id.btnPhoto)
btnCall.setOnClickListener {
val uri = Uri.parse("tel:010-1234-5678")
startActivity(Intent(Intent.ACTION_DIAL, uri))
}
btnWeb.setOnClickListener {
val uri = Uri.parse("http://daum.net")
startActivity(Intent(Intent.ACTION_VIEW, uri))
}
btnGoogle.setOnClickListener {
val uri = Uri.parse("https://maps.google.com/maps?q="
+ 35.86606 + "," + 128.5938 + "&z=15")
startActivity(Intent(Intent.ACTION_VIEW, uri))
}
btnSearch.setOnClickListener {
val intent = Intent(Intent.ACTION_WEB_SEARCH)
intent.putExtra(SearchManager.QUERY, "안드로이드")
startActivity(intent)
}
btnSms.setOnClickListener {
val intent = Intent(Intent.ACTION_SENDTO)
intent.putExtra("sms_body", "안녕하세요?")
intent.setData(Uri.parse("smsto:010-1234-5678"))
startActivity(intent)
}
btnPhoto.setOnClickListener {
startActivity(Intent(MediaStore.ACTION_IMAGE_CAPTURE))
}
}
}
- 액티비티
3. 액티비티와 인텐트
package kr.jeongmo.a0508_newproject03
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnGoSecond = findViewById<Button>(R.id.btnGoSecond)
btnGoSecond.setOnClickListener {
val intent = Intent(applicationContext, SecondActivity::class.java)
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:background="@color/white"
tools:context=".MainActivity">
<Button
android:id="@+id/btnGoSecond"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="새 화면 열기"/>
</LinearLayout>
package kr.jeongmo.a0508_newproject03
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val btnGoThird = findViewById<Button>(R.id.btnGoThird)
btnGoThird.setOnClickListener {
val intent = Intent(applicationContext, ThirdActivity::class.java)
startActivity(intent)
}
val btnReturn1 = findViewById<Button>(R.id.btnReturn1)
btnReturn1.setOnClickListener {
finish()
}
}
}
<?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:background="@color/teal_200"
tools:context=".SecondActivity">
<Button
android:id="@+id/btnGoThird"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="새 화면 열기"/>
<Button
android:id="@+id/btnReturn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"/>
</LinearLayout>
package kr.jeongmo.a0508_newproject03
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class ThirdActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_third)
val btnReturn2 = findViewById<Button>(R.id.btnReturn2)
btnReturn2.setOnClickListener {
finish()
}
}
}
<?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:background="@color/teal_700"
tools:context=".ThirdActivity">
<Button
android:id="@+id/btnReturn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="돌아가기"/>
</LinearLayout>
공부 과정을 정리한 것이라 내용이 부족할 수 있습니다.
부족한 내용은 추가 자료들로 보충해주시면 좋을 것 같습니다.
읽어주셔서 감사합니다 :)
'Dev > Android' 카테고리의 다른 글
Android (어댑터뷰, 리스트뷰, 동적으로 항목 추가, 그리드뷰) (0) | 2024.05.10 |
---|---|
Android (액티비티 생명주기, 로그캣, 액티비티 테스트, 안드로이드 4대 구성요소, 디자인탭 활용, 액티비티와 프래그먼트) (0) | 2024.05.09 |
Android (명화 선호도 투표 프로젝트 기능 추가 ) (0) | 2024.05.07 |
Android (액티비티와 인텐트, 인텐트 프로젝트 + 레이팅바, 명화 프로젝트) (0) | 2024.05.03 |
Android (파일 처리 응용, SD카드 폴더 / 파일 생성 및 삭제, SD카드 폴더 / 파일 목록 출력, 파일처리 - 프로젝트, 이미지 번호 표시) (0) | 2024.05.02 |