Android Studio/App Components

Explicit Intent

shDev 2026. 3. 2. 20:30

 

🔷 Intent란?

Android의 핵심 통신 메커니즘으로, 앱 내부 또는 앱 간의 컴포넌트끼리 작업을 요청할 때 사용한다.

Intent로 할 수 있는 것들:

  • Activity 시작
  • Service 실행
  • 브로드캐스트 메시지 전송

Intent는 Explicit(명시적)Implicit(암시적) 두 가지 종류가 있다.


🔷 Explicit Intent란?

목적지(컴포넌트)를 명확하게 지정해서 호출하는 Intent. 주로 같은 앱 내에서 Activity 간 이동할 때 사용한다.

Main Activity  ──[Explicit Intent]──▶  Second Activity

🔷 사용법

기본 구조

val intent = Intent(현재 Context, 목적지::class.java)
startActivity(intent)

실제 예시 — MainActivity에서 SecondActivity로 이동

// MainActivity.kt

fun goToSecondActivity() {
    val intent = Intent(this, SecondActivity::class.java)
    startActivity(intent)
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val btn = findViewById<Button>(R.id.button)
    btn.setOnClickListener {
        goToSecondActivity()
    }
}

🔷 Intent 생성 시 두 가지 파라미터

파라미터 의미 예시

첫 번째 현재 위치 (Context) this (현재 Activity)
두 번째 목적지 (Class) SecondActivity::class.java

💡 Context란 현재 앱/컴포넌트의 환경 정보를 담고 있는 객체로, Activity 내에서는 this로 접근한다.


🔷 Explicit vs Implicit Intent 비교

구분 Explicit Intent Implicit Intent

목적지 지정 클래스명으로 명확히 지정 작업(Action)만 지정
주 사용처 같은 앱 내 화면 이동 외부 앱 기능 호출
예시 A화면 → B화면 이동 지도 앱 열기, 전화 걸기

🔷 ⭐ 핵심 포인트

  1. Explicit Intent = 목적지가 명확한 Intent — 클래스명을 직접 지정하므로 같은 앱 내부 이동에 적합하다.
  2. Intent 객체 생성 시 Context(현재 위치)목적지 Class 두 가지를 반드시 전달해야 한다.
  3. startActivity(intent)를 호출해야 실제로 화면이 전환된다.
  4. 데이터 전달이 필요하다면 intent.putExtra("key", value)로 데이터를 함께 넘길 수 있다. ← 강의엔 없지만 실무에서 매우 중요!
// 데이터 전달 (심화)
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("username", "Alice")
startActivity(intent)

// SecondActivity에서 받기
val name = intent.getStringExtra("username")

'Android Studio > App Components' 카테고리의 다른 글

예제  (0) 2026.03.02
Android Manifest  (0) 2026.03.02
Implicit Intent  (0) 2026.03.02
Android Activity Lifecycle  (0) 2026.03.02