티스토리 뷰
Reference Chatgpt
Problem
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? startActivity 시 런타임 예외 발생할떄 해결법
Solution
`startActivity()` 메서드를 호출할 때 "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag"라는 오류가 발생하는 것은, `Activity`의 컨텍스트가 아닌 다른 컨텍스트(예: `Application` 또는 `Service`)에서 `startActivity()`를 호출했기 때문입니다. 이러한 경우, `FLAG_ACTIVITY_NEW_TASK` 플래그를 설정해야 합니다.
### 해결 방법
1. **FLAG_ACTIVITY_NEW_TASK 추가하기**:
- `Intent`에 `FLAG_ACTIVITY_NEW_TASK` 플래그를 추가하여 해결할 수 있습니다. 이렇게 하면 새로운 작업(Task)에서 `Activity`를 시작하게 됩니다.
### 예시 코드
```kotlin
// Intent 생성
val intent = Intent(context, DebugActivity::class.java).apply {
putExtra("key", "value") // 추가 데이터 전달
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // 새로운 Task에서 Activity 시작
}
// Activity 시작
context.startActivity(intent)
```
### 2. **어디에서 호출하는지 확인하기**
- `startActivity()`를 호출하는 코드가 **`Application`** 클래스나 **`BroadcastReceiver`** 같은 비-액티비티 컴포넌트에서 실행되고 있는지 확인하세요. 이러한 경우, `Context`를 `Activity`에서 제공하는 것이 아니기 때문에 반드시 `FLAG_ACTIVITY_NEW_TASK`를 추가해야 합니다.
### 3. **위치에 따른 코드 예시**
- **`BroadcastReceiver`에서 Activity 시작**:
```kotlin
class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val debugIntent = Intent(context, DebugActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // 플래그 추가
}
context.startActivity(debugIntent)
}
}
```
- **`Service`에서 Activity 시작**:
```kotlin
class MyService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val debugIntent = Intent(this, DebugActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // 플래그 추가
}
startActivity(debugIntent)
return START_STICKY
}
}
```
### 4. **Activity Context에서 호출하는 경우**
- 만약 `Activity`에서 `startActivity()`를 호출할 경우, `FLAG_ACTIVITY_NEW_TASK`는 필요하지 않습니다. 그냥 `startActivity(intent)`로 호출하면 됩니다.
### 요약
- **비-액티비티 컨텍스트**에서 `startActivity()`를 호출할 때는 반드시 `FLAG_ACTIVITY_NEW_TASK` 플래그를 추가해야 합니다.
- 해당 플래그를 `Intent`에 추가하면 새로운 Task에서 `Activity`가 시작되어 런타임 예외를 피할 수 있습니다.
Question
Android 에서 Task 개념, 레이어 같은 것인지..?
context 종류 - application, activity
'IT > Android' 카테고리의 다른 글
보일러플레이트 코드 (0) | 2024.07.10 |
---|---|
Android ABI (0) | 2024.02.08 |
[KOTLIN] Exception 강제 발생 시키기 (0) | 2023.11.22 |
TODO:: [Kotlin] ContentResolver (0) | 2023.10.17 |
[Kotlin] 코틀린 reified 키워드 (0) | 2023.10.17 |
- Total
- Today
- Yesterday
- TKD
- https://kotlinlang.org/docs/object-declarations.html
- 클라우드 네이티브 애플리케이션 디자인 패턴
- 코틀린
- Repository pattern
- InShot
- 코딩테스트
- 청년내일채움공제
- 아이폰영상편집어플후기
- 레포지토리패턴
- 디자인패턴
- Kotlin
- 통신패턴
- 수학 #다이나믹프로그래밍 #게임이론
- 맥북개발환경
- 내채공만기
- Vlogr
- gitlab_ssh_permission_denied
- EH
- android
- VLLO
- AdobeClip
- 프로그래머스
- 안드로이드
- 내채공
- 알고리즘
- 쇼코의미소
- 동기메시징기술
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |