Android Studio

Android | Activity, Fragment, View, Intent, Context

ziziDev 2024. 12. 11. 19:51
๋ฐ˜์‘ํ˜•

 

๐Ÿ”ฅ 1. ๊ฐœ๋… ์„ค๋ช…

๐Ÿ“ฑ 1. Activity

  • ์ •์˜: ํ•˜๋‚˜์˜ ํ™”๋ฉด์„ ๋‹ด๋‹นํ•˜๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์˜ ๊ธฐ๋ณธ ๋‹จ์œ„.
  • ์—ญํ• : ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฐฝ ์—ญํ• ์„ ํ•จ.
  • ์ƒ๋ช…์ฃผ๊ธฐ: onCreate, onStart, onResume, onPause, onStop, onDestroy ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Œ.
  • ์˜ˆ์‹œ: ์นด์นด์˜คํ†ก์˜ ์ฑ„ํŒ… ๋ชฉ๋ก ํ™”๋ฉด, ์ฑ„ํŒ…๋ฐฉ ํ™”๋ฉด์ด ๊ฐ๊ฐ ๋ณ„๋„์˜ Activity๋กœ ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ์Œ.

๐Ÿ“˜ ์ค‘์š” ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ์„ค๋ช…

onCreate() Activity ์ƒ์„ฑ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ๋ทฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋จ.
onStart() Activity๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์ด๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋จ.
onResume() ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋จ.
onPause() ๋‹ค๋ฅธ ํ™”๋ฉด์œผ๋กœ ์ „ํ™˜๋  ๋•Œ ํ˜ธ์ถœ๋จ (ex. ์ „ํ™” ์ˆ˜์‹ ).
onStop() ๋‹ค๋ฅธ Activity๋กœ ์™„์ „ํžˆ ์ „ํ™˜๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋จ.
onDestroy() Activity๊ฐ€ ์™„์ „ํžˆ ์ข…๋ฃŒ๋  ๋•Œ ํ˜ธ์ถœ๋จ.

๐Ÿ”ฅ 2. Fragment

  • ์ •์˜: Activity ๋‚ด์—์„œ ํ™”๋ฉด์˜ ์ผ๋ถ€๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ์ž‘์€ ๋ชจ๋“ˆํ™”๋œ ํ™”๋ฉด.
  • ์—ญํ• : ํ•œ ๊ฐœ์˜ Activity์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ Fragment๋ฅผ ๋ถ™์ด๊ฑฐ๋‚˜, ๊ต์ฒดํ•˜๋ฉด์„œ ๋™์  ํ™”๋ฉด ์ „ํ™˜์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ.
  • ํ™œ์šฉ:
    • ํ•˜๋‚˜์˜ Activity์— **ํƒญ ๋ ˆ์ด์•„์›ƒ(TabLayout)**์„ ๊ตฌํ˜„ํ•  ๋•Œ ๊ฐ ํƒญ์— Fragment๋ฅผ ์—ฐ๊ฒฐ.
    • **๋ทฐํŽ˜์ด์ €(ViewPager)**์—์„œ ์ขŒ์šฐ๋กœ ์Šฌ๋ผ์ด๋“œํ•  ๋•Œ Fragment๋ฅผ ์‚ฌ์šฉํ•จ.

๐Ÿ“˜ ์ค‘์š” ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ์„ค๋ช…

onAttach() Fragment๊ฐ€ Activity์— ์—ฐ๊ฒฐ๋  ๋•Œ ํ˜ธ์ถœ๋จ.
onCreateView() Fragment์˜ UI๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋จ.
onViewCreated() View๊ฐ€ ์™„์ „ํžˆ ์ƒ์„ฑ๋œ ํ›„ ํ˜ธ์ถœ๋จ.
onDestroyView() View๊ฐ€ ํŒŒ๊ดด๋  ๋•Œ ํ˜ธ์ถœ๋จ.

๐Ÿ”ฅ 3. View

  • ์ •์˜: ํ™”๋ฉด์˜ ๊ตฌ์„ฑ์š”์†Œ๋กœ, ๋ฒ„ํŠผ, ํ…์ŠคํŠธ, ์ด๋ฏธ์ง€ ๋“ฑ ์‚ฌ์šฉ์ž๊ฐ€ ๋ณผ ์ˆ˜ ์žˆ๋Š” UI ์š”์†Œ.
  • ์—ญํ• : ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž…๋ ฅ, ํด๋ฆญ, ๋“œ๋ž˜๊ทธ ๊ฐ™์€ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•จ.
  • ์ข…๋ฅ˜:
    • TextView: ํ…์ŠคํŠธ ํ‘œ์‹œ
    • Button: ๋ฒ„ํŠผ ์ƒ์„ฑ
    • EditText: ์‚ฌ์šฉ์ž ์ž…๋ ฅ ํ…์ŠคํŠธ
    • ImageView: ์ด๋ฏธ์ง€ ํ‘œ์‹œ
    • RecyclerView: ๋ชฉ๋ก ํ˜•ํƒœ์˜ ์Šคํฌ๋กค ๋ทฐ

๐Ÿ“˜ ์ค‘์š” ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ์„ค๋ช…

setOnClickListener() ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•˜๋Š” ๋ฉ”์„œ๋“œ.
setVisibility() View์˜ ๋ณด์ด๊ธฐ/์ˆจ๊ธฐ๊ธฐ๋ฅผ ์ œ์–ด.
setEnabled() View์˜ ํ™œ์„ฑํ™”/๋น„ํ™œ์„ฑํ™” ์ œ์–ด.

๐Ÿ”ฅ 4. Intent

  • ์ •์˜: Activity ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ณ , ์ƒˆ๋กœ์šด Activity๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด.
  • ์—ญํ• : Activity ๊ฐ„ ํ™”๋ฉด ์ „ํ™˜, ์„œ๋น„์Šค ์‹คํ–‰, ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ์†ก์ˆ˜์‹ ์— ์‚ฌ์šฉ.
  • ์ข…๋ฅ˜:
    • Explicit Intent (๋ช…์‹œ์  ์ธํ…ํŠธ): ํŠน์ • Activity๋ฅผ ๋ช…ํ™•ํžˆ ์ง€์ •.
    • Implicit Intent (์•”์‹œ์  ์ธํ…ํŠธ): ์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ, ์›น๋ธŒ๋ผ์šฐ์ € ์—ด๊ธฐ ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ.

๐Ÿ“˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ์„ค๋ช…

startActivity() ๋‹ค๋ฅธ Activity๋ฅผ ์‹œ์ž‘ํ•จ.
putExtra() ๋‹ค๋ฅธ Activity์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•จ.
getExtras() ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉํ•จ.

๐Ÿ”ฅ 5. Context

  • ์ •์˜: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•œ ์ •๋ณด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค.
  • ์—ญํ• : ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ, ์•กํ‹ฐ๋น„ํ‹ฐ ์‹œ์ž‘, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ๋“ฑ ๋‹ค์–‘ํ•œ ์ž‘์—…์— ์‚ฌ์šฉ๋จ.
  • ์ข…๋ฅ˜:
    • Activity Context: Activity ์ž์ฒด๊ฐ€ Context์ž„.
    • Application Context: ์•ฑ์˜ ์ „๋ฐ˜์ ์ธ Context.
  • ํ™œ์šฉ:
    • getApplicationContext(): ์•ฑ ์ „์ฒด์˜ ์ปจํ…์ŠคํŠธ ์ ‘๊ทผ.
    • this: Activity์—์„œ ํ˜„์žฌ์˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์ฐธ์กฐํ•  ๋•Œ ์‚ฌ์šฉ.

 

๐Ÿ”ฅ 2. ์‹ค์Šต: ๋‘ ๊ฐœ์˜ Activity๋กœ ํ™”๋ฉด ์ „ํ™˜ ์•ฑ ๋งŒ๋“ค๊ธฐ

package com.example.myapp

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)
        
        // 1. ๋ฒ„ํŠผ ์—ฐ๊ฒฐ (๋ทฐ ๋ฐ”์ธ๋”ฉ)
        val btnGoToSecond = findViewById<Button>(R.id.btnGoToSecond)
        
        // 2. ํด๋ฆญ ๋ฆฌ์Šค๋„ˆ ์—ฐ๊ฒฐ
        btnGoToSecond.setOnClickListener {
            // 3. Intent๋กœ ํ™”๋ฉด ์ „ํ™˜
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
    }
}

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnGoToSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to Second Activity" />
</LinearLayout>

 


package com.example.myapp

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)
        
        // 1. ๋ฒ„ํŠผ ์—ฐ๊ฒฐ (๋ทฐ ๋ฐ”์ธ๋”ฉ)
        val btnGoBack = findViewById<Button>(R.id.btnGoBack)
        
        // 2. ํด๋ฆญ ๋ฆฌ์Šค๋„ˆ ์—ฐ๊ฒฐ
        btnGoBack.setOnClickListener {
            // 3. ํ˜„์žฌ Activity ์ข…๋ฃŒ (MainActivity๋กœ ๋ณต๊ท€)
            finish()
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnGoBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go Back to Main" />
</LinearLayout>

 

https://developer.android.com/reference/android/content/Context

 

Context  |  Android Developers

 

developer.android.com

https://developer.android.com/reference/android/app/Activity?_gl=1*17lxktt*_up*MQ..*_ga*MTE1ODU4ODE2LjE3MzM4NzQzODI.*_ga_6HH9YJMN9M*MTczMzg3NDM4Ni4xLjAuMTczMzg3NDM4Ni4wLjAuMTY4ODM3MTIyMA..#Fragments

 

Activity  |  Android Developers

 

developer.android.com

https://developer.android.com/reference/android/content/Intent

 

Intent  |  Android Developers

 

developer.android.com

 

๋ฐ˜์‘ํ˜•