๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Android Studio

[Android ์•ฑ๊ฐœ๋ฐœ ์ž…๋ฌธ] ์•กํ‹ฐ๋น„ํ‹ฐ ๊ฐ„์˜ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ (# ์ถ”๊ฐ€ํ•˜๊ธฐ)

by ์ ผ์ ผ39 2024. 3. 21.

1. ๋ฐ์ดํ„ฐ ์ „๋‹ฌ

    - ์ธํ…ํŠธ์— ์ปดํฌ๋„ŒํŠธ ์‹คํ–‰์„ ์š”์ฒญํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ์ „๋‹ฌํ•˜๋ ค๋ฉด ์—‘์ŠคํŠธ๋ผ ๋ฐ์ดํ„ฐ(์ธํ…ํŠธ์— ๋‹ด๋Š” ๋ถ€๊ฐ€์ •๋ณด) ์ด์šฉ

    - ์ด๋ฆ„๊ณผ ๊ฐ’์˜ ์Œ์œผ๋กœ ๋œ ์ •๋ณด๋ฅผ ์ „๋‹ฌ

 

2. Extras์— ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ

Intent putExtra(String name, int value)
Intent putExtra(String name, String value)
Intent putExtra(String name, boolean value)

 

3. Extras์— ์ €์žฅ๋œ ๊ฐ’์„ ์ฝ๋Š” ๋ฉ”์†Œ๋“œ

int getIntExtra(String name, int defaultValue)
String getStringExtra(String name)
boolean getBooleanExtra(String name, boolean defaultValue)

 

 

(๋ฐ์ดํ„ฐ ์ž…๋ ฅ๋ฐ›๊ณ  ์„ธ๋ฒˆ์งธ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๋ฐ์ดํ„ฐ ์ „๋‹ฌํ•˜๋Š” ์ฝ”๋“œ)

1) activity_first.xml์—์„œ ์ฝ”๋“œ ์ถ”๊ฐ€

    (๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” EditText ์œ„์ ฏ๊ณผ ์„ธ๋ฒˆ์งธ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ํ˜ธ์ถœํ•  Button ์œ„์ ฏ์„ ์ˆ˜ํ‰์œผ๋กœ ๋ฐฐ์—ด์‹œํ‚ค๋Š”
        LinearLayout ์ถ”๊ฐ€)

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/edit_data"
            android:layout_weight="1"
            android:hint="๋ฐ์ดํ„ฐ ์ž…๋ ฅ"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="์„ธ๋ฒˆ์งธ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๋ฐ์ดํ„ฐ ์ „๋‹ฌ"
            android:id="@+id/buttonThirdActivity"/>
    </LinearLayout>

 

2) FirstActivity.kt ํŒŒ์ผ์— ์„ธ ๋ฒˆ์งธ ์•กํ‹ฐ๋น„ํ‹ฐ ์‹œ์ž‘์‹œํ‚ค๊ธฐ ์œ„ํ•œ ๋ช…์‹œ์  ์ธํ…ํŠธ ๊ฐ์ฒด ์ƒ์„ฑ ์ฝ”๋“œ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ

    (ํ•ด๋‹น ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์„ ๋•Œ ์ธํ…ํŠธ ๊ฐ์ฒด ์ƒ์„ฑ, id๊ฐ€ edit_data์ธ EditText ๊ฐ์ฒด์— ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด ๊ฐ€์ ธ์™€์„œ
        ์ด๋ฅผ ์•ž์—์„œ ์ƒ์„ฑํ•œ ์ธํ…ํŠธ ๊ฐ์ฒด์˜ Extra์— "dataFromFirstActivity" ์ด๋ฆ„์œผ๋กœ ์„ค์ •,
            startActivity() ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ธํ…ํŠธ ๊ฐ์ฒด ์ „๋‹ฌ)

        //๋ช…์‹œ์  ์ธํ…ํŠธ
        val btn_call3 = findViewById<Button>(R.id.buttonThirdActivity)
        btn_call3.setOnClickListener {
            //id๊ฐ€ edit_data์ธ EditText ๊ฐ์ฒด
            val edit_text = findViewById<EditText>(R.id.edit_data)
            //๊ทธ ๊ฐ์ฒด ์•ˆ์— ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด ๊ฐ’์„ ๊ฐ€์ ธ์˜ด
            val strData = edit_text.text.toString()
            //์ธํ…ํŠธ ๊ฐ์ฒด ์ƒ์„ฑ (์„ธ๋ฒˆ์งธ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์—ฌ๊ธฐ์„œ ์‹คํ–‰ํ• ๊ฑฐ๋‹ค)
            val intent = Intent(this, ThirdActivity::class.java)
            //๊ฐ€์ ธ์˜จ ๋ฌธ์ž์—ด ๊ฐ’์„ ์•ž์—์„œ ์ƒ์„ฑํ•œ ์ธํ…ํŠธ ๊ฐ์ฒด์˜ Extra์— ์„ค์ • (์ด๋ฆ„์€ dataFromFirstActivity๋กœ ์„ค์ •)
            intent.putExtra("dataFromFirstActivity",strData)
            //startActivity()์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ธํ…ํŠธ ๊ฐ์ฒด ์ „๋‹ฌ
            startActivity(intent)

        }

3) activity_third.xml ํŒŒ์ผ ์ˆ˜์ •

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ThirdActivity"
    android:gravity="center"
    android:orientation="vertical">

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="์„ธ๋ฒˆ์งธ ์•กํ‹ฐ๋น„ํ‹ฐ์ž…๋‹ˆ๋‹ค."
      android:id="@+id/textView2"/>
  <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:inputType="text"/>
  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="๋‹ซ๊ธฐ"
      android:id="@+id/buttonThirdActivity"/>

  </LinearLayout>

4) ThirdActivity.kt ํŒŒ์ผ์— ์ฝ”๋“œ ์ถ”๊ฐ€

    (ThirdActivity๋กœ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ Extra์—์„œ ๊ฐ€์ ธ์™€ EditText๋ทฐ์— ํ‘œ์‹œ)
    (๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜๋ฉด finish() ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•ด ํ˜„์žฌ์˜ ์•กํ‹ฐ๋น„ํ‹ฐ ์ข…๋ฃŒ๋˜๋Š” ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ธฐ ์ถ”๊ฐ€)

package com.example.acrivityintent2

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class ThirdActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_third)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        
		//"dataFromFirstActivity" ๋Š” ํ‚ค๊ฐ’. ํ‚ค๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ
        val strData = intent.getStringExtra("dataFromFirstActivity")
        val editText = findViewById<EditText>(R.id.editText)
        //set -> ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์คŒ. (๋ฟŒ๋ ค์คŒ?)
        editText.setText(strData)

        val btn_close = findViewById<Button>(R.id.buttonThirdActivity)
        btn_close.setOnClickListener {
            finish()
        }
    }
}