알림에서 활동 시작
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
알림에서 활동을 시작할 때 사용자의
탐색 환경을 조성하는 데 도움이 됩니다. 뒤로 버튼을 탭하면 사용자가 뒤로 이동해야 함
앱의 일반적인 워크플로를 통해 홈 화면으로 이동한 다음 최근 항목
활동을 별도의 작업으로 표시해야 합니다. 탐색 메뉴 유지
새로운 작업에서 활동을 시작할 수 있습니다.
알림의 탭 동작을 설정하는 기본 방법은 다음에 설명되어 있습니다.
기본
알림을 받을 수 있습니다.
이 페이지에서는
PendingIntent
을(를) 위한
새 할 일을 생성하고 다시 돌아올 수 있습니다.
스택을 참고하세요. 실행 방법
시작하는 활동의 유형에 따라 다릅니다.
- 정규 액티비티
- 앱의 일반 UX 흐름의 일부로 존재하는 활동입니다. 날짜
사용자가 알림에서 Activity에 도착할 때 새 작업은
완전한 백 스택을 포함하여 사용자가 뒤로 버튼을 탭하여 탐색할 수 있음
앱 계층 구조 위로 올라갑니다.
- 특수 액티비티
- 사용자는 알림에서 시작할 때만 이 활동을 볼 수 있습니다.
이 액티비티는 알림 UI를 확장하는 정보를 제공하여
알림 자체에 표시하기 어렵습니다. 이 활동에는
백 스택
일반 활동 PendingIntent 설정
알림에서 일반적인 활동을 시작하려면 PendingIntent
을(를) 설정하세요.
TaskStackBuilder
사용 중
다음과 같이 새로운 백 스택을 생성합니다.
앱의 활동 계층 구조 정의
액티비티의 자연스러운 계층 구조를 정의하기 위해
android:parentActivityName
드림
속성을 각 <activity>
에
요소를 선언할 수 있습니다. 아래 예를 참고하세요.
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- MainActivity is the parent for ResultActivity. -->
<activity
android:name=".ResultActivity"
android:parentActivityName=".MainActivity" />
...
</activity>
백 스택이 있는 PendingIntent 작성
활동의 백 스택이 포함된 활동을 시작하려면
TaskStackBuilder
인스턴스 및
addNextIntentWithParentStack()
,
다음을 위한 Intent
를
선택합니다.
설명한 대로 각 활동의 상위 활동을 정의하기만 하면 됩니다.
앞서 kubectl 명령어
getPendingIntent()
드림
전체 백 스택이 포함된 PendingIntent
를 수신합니다.
Kotlin
// Create an Intent for the activity you want to start.
val resultIntent = Intent(this, ResultActivity::class.java)
// Create the TaskStackBuilder.
val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
// Add the intent, which inflates the back stack.
addNextIntentWithParentStack(resultIntent)
// Get the PendingIntent containing the entire back stack.
getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}
자바
// Create an Intent for the activity you want to start.
Intent resultIntent = new Intent(this, ResultActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back
// stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack.
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
필요한 경우 다음을 호출하여 스택의 Intent
객체에 인수를 추가할 수 있습니다.
TaskStackBuilder.editIntentAt()
이는 백 스택의 활동이 실제로 로드되도록 하기 위해
사용자가 이동할 때 의미 있는 데이터를 표시합니다.
그런 다음 평소와 같이 알림에 PendingIntent
를 전달할 수 있습니다.
Kotlin
val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
setContentIntent(resultPendingIntent)
...
}
with(NotificationManagerCompat.from(this)) {
notify(NOTIFICATION_ID, builder.build())
}
자바
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(resultPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
특수 활동 PendingIntent 설정
알림에서 시작되는 특수 활동은 뒤로 갈 필요가 없기 때문
다음과 같이 호출하여 PendingIntent
를 만들 수 있습니다.
getActivity()
하지만, 매니페스트에서 적절한 작업 옵션을 정의하세요.
-
매니페스트에서 다음 속성을
<activity>
요소
-
android:taskAffinity=""
-
또한
FLAG_ACTIVITY_NEW_TASK
플래그를 사용하지 않는 경우 이 속성을 비워두면
이 활동은 앱의 기본 작업으로 이동하지 않습니다. 모든 문자
앱의 기본 어피니티가 있는 기존 작업은
있습니다.
-
android:excludeFromRecents="true"
-
사용자가
다시 이동할 수 없습니다.
예를 들면 다음과 같습니다.
<activity
android:name=".ResultActivity"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true">
</activity>
-
알림을 빌드하고 발행합니다.
<ph type="x-smartling-placeholder">
</ph>
-
다음을 시작하는
Intent
를 만듭니다.
Activity
입니다.
-
다음과 같이 새로운 빈 작업에서 시작하도록
Activity
를 설정합니다.
통화
setFlags()
플래그 FLAG_ACTIVITY_NEW_TASK
및
FLAG_ACTIVITY_CLEAR_TASK
입니다.
-
다음을 호출하여
PendingIntent
를 만듭니다.
getActivity()
입니다.
예를 들면 다음과 같습니다.
Kotlin
val notifyIntent = Intent(this, ResultActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val notifyPendingIntent = PendingIntent.getActivity(
this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
자바
Intent notifyIntent = new Intent(this, ResultActivity.class);
// Set the Activity to start in a new, empty task.
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Create the PendingIntent.
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
- 평소와 같이
PendingIntent
를 알림에 전달합니다.
Kotlin
val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
setContentIntent(notifyPendingIntent)
...
}
with(NotificationManagerCompat.from(this)) {
notify(NOTIFICATION_ID, builder.build())
}
자바
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(notifyPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
다양한 작업 옵션 및 백 스택 방법에 관한 자세한 내용은
작업 및 백 스택을 참고하세요.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Start an Activity from a Notification\n\nWhen you start an activity from a notification, you must preserve the user's\nexpected navigation experience. Tapping the Back button must take the user back\nthrough the app's normal work flow to the Home screen, and opening the Recents\nscreen must show the activity as a separate task. To preserve this navigation\nexperience, start the activity in a fresh task.\n\nThe basic approach to set the tap behavior for your notification is described in\n[Create a basic\nnotification](/develop/ui/views/notifications/build-notification#SimpleNotification).\nThis page describes how to set up a\n[`PendingIntent`](/reference/android/app/PendingIntent) for your\nnotification's action so it creates a fresh [task and back\nstack](/guide/components/activities/tasks-and-back-stack). How you do this\ndepends on which type of activity you're starting:\n\nRegular activity\n: This is an activity that exists as a part of your app's normal UX flow. When\n the user arrives in the activity from the notification, the new task must\n include a complete back stack, letting the user tap the Back button to navigate\n up the app hierarchy.\n\nSpecial activity\n: The user only sees this activity if it's started from a notification. In a\n sense, this activity extends the notification UI by providing information that\n is difficult to display in the notification itself. This activity doesn't need a\n back stack.\n\nSet up a regular activity PendingIntent\n---------------------------------------\n\nTo start a regular activity from your notification, set up the `PendingIntent`\nusing [`TaskStackBuilder`](/reference/androidx/core/app/TaskStackBuilder)\nso that it creates a new back stack as follows.\n\n### Define your app's Activity hierarchy\n\nDefine the natural hierarchy for your activities by adding the\n[`android:parentActivityName`](/guide/topics/manifest/activity-element#parent)\nattribute to each [`\u003cactivity\u003e`](/guide/topics/manifest/activity-element)\nelement in your app manifest file. See the following example: \n\n```xml\n\u003cactivity\n android:name=\".MainActivity\"\n android:label=\"@string/app_name\" \u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.intent.action.MAIN\" /\u003e\n \u003ccategory android:name=\"android.intent.category.LAUNCHER\" /\u003e\n \u003c/intent-filter\u003e\n\u003c/activity\u003e\n\u003c!-- MainActivity is the parent for ResultActivity. --\u003e\n\u003cactivity\n android:name=\".ResultActivity\"\n android:parentActivityName=\".MainActivity\" /\u003e\n ...\n\u003c/activity\u003e\n```\n\n### Build a PendingIntent with a back stack\n\nTo start an activity that includes a back stack of activities, create an\ninstance of `TaskStackBuilder` and call\n[`addNextIntentWithParentStack()`](/reference/androidx/core/app/TaskStackBuilder#addNextIntentWithParentStack(android.content.Intent)),\npassing it the [`Intent`](/reference/android/content/Intent) for the\nactivity you want to start.\n\nAs long as you define the parent activity for each activity as described\nearlier, you can call\n[`getPendingIntent()`](/reference/androidx/core/app/TaskStackBuilder#getPendingIntent(int,int))\nto receive a `PendingIntent` that includes the entire back stack. \n\n### Kotlin\n\n```kotlin\n// Create an Intent for the activity you want to start.\nval resultIntent = Intent(this, ResultActivity::class.java)\n// Create the TaskStackBuilder.\nval resultPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {\n // Add the intent, which inflates the back stack.\n addNextIntentWithParentStack(resultIntent)\n // Get the PendingIntent containing the entire back stack.\n getPendingIntent(0,\n PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)\n}\n```\n\n### Java\n\n```java\n// Create an Intent for the activity you want to start.\nIntent resultIntent = new Intent(this, ResultActivity.class);\n// Create the TaskStackBuilder and add the intent, which inflates the back\n// stack.\nTaskStackBuilder stackBuilder = TaskStackBuilder.create(this);\nstackBuilder.addNextIntentWithParentStack(resultIntent);\n// Get the PendingIntent containing the entire back stack.\nPendingIntent resultPendingIntent =\n stackBuilder.getPendingIntent(0,\n PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);\n```\n\nIf necessary, you can add arguments to `Intent` objects in the stack by calling\n[`TaskStackBuilder.editIntentAt()`](/reference/androidx/core/app/TaskStackBuilder#editIntentAt(int)).\nThis is sometimes necessary to ensure that an activity in the back stack\ndisplays meaningful data when the user navigates to it.\n\nThen you can pass the `PendingIntent` to the notification as usual: \n\n### Kotlin\n\n```kotlin\nval builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {\n setContentIntent(resultPendingIntent)\n ...\n}\nwith(NotificationManagerCompat.from(this)) {\n notify(NOTIFICATION_ID, builder.build())\n}\n```\n\n### Java\n\n```java\nNotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);\nbuilder.setContentIntent(resultPendingIntent);\n...\nNotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);\nnotificationManager.notify(NOTIFICATION_ID, builder.build());\n```\n\nSet up a special activity PendingIntent\n---------------------------------------\n\nBecause a special activity that starts from a notification doesn't need a back\nstack, you can create the `PendingIntent` by calling\n[`getActivity()`](/reference/android/app/PendingIntent#getActivity(android.content.Context,%20int,%20android.content.Intent,%20int)).\nHowever, define the appropriate task options in the manifest.\n\n1. In your manifest, add the following attributes to the `\u003cactivity\u003e` element.\n\n\n [android:taskAffinity](/guide/topics/manifest/activity-element#aff)`=\"\"`\n :\n Combined with the\n [FLAG_ACTIVITY_NEW_TASK](/reference/android/content/Intent#FLAG_ACTIVITY_NEW_TASK)\n flag that you use in code, set this attribute blank to ensure\n this activity doesn't go into the app's default task. Any\n existing tasks that have the app's default affinity aren't\n affected.\n\n\n [android:excludeFromRecents](/guide/topics/manifest/activity-element#exclude)`=\"true\"`\n :\n Excludes the new task from the Recents screen so that the user\n can't accidentally navigate back to it.\n\n\n This is shown in the following example: \n\n ```xml\n \u003cactivity\n android:name=\".ResultActivity\"\n android:launchMode=\"singleTask\"\n android:taskAffinity=\"\"\n android:excludeFromRecents=\"true\"\u003e\n \u003c/activity\u003e\n ```\n2. Build and issue the notification:\n 1. Create an `Intent` that starts the [Activity](/reference/android/app/Activity).\n 2. Set the `Activity` to start in a new, empty task by calling [setFlags()](/reference/android/content/Intent#setFlags(int)) with the flags `FLAG_ACTIVITY_NEW_TASK` and [FLAG_ACTIVITY_CLEAR_TASK](/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TASK).\n 3. Create a `PendingIntent` by calling `getActivity()`.\n\n\n This is shown in the following example: \n\n ### Kotlin\n\n ```kotlin\n val notifyIntent = Intent(this, ResultActivity::class.java).apply {\n flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK\n }\n val notifyPendingIntent = PendingIntent.getActivity(\n this, 0, notifyIntent,\n PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE\n )\n ```\n\n ### Java\n\n ```java\n Intent notifyIntent = new Intent(this, ResultActivity.class);\n // Set the Activity to start in a new, empty task.\n notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK\n | Intent.FLAG_ACTIVITY_CLEAR_TASK);\n // Create the PendingIntent.\n PendingIntent notifyPendingIntent = PendingIntent.getActivity(\n this, 0, notifyIntent,\n PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE\n );\n ```\n3. Pass the `PendingIntent` to the notification as usual: \n\n ### Kotlin\n\n ```kotlin\n val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {\n setContentIntent(notifyPendingIntent)\n ...\n }\n with(NotificationManagerCompat.from(this)) {\n notify(NOTIFICATION_ID, builder.build())\n }\n ```\n\n ### Java\n\n ```java\n NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);\n builder.setContentIntent(notifyPendingIntent);\n ...\n NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);\n notificationManager.notify(NOTIFICATION_ID, builder.build());\n ```\n\nFor more information about the various task options and how the back stack\nworks, see [Tasks and the back stack](/guide/components/activities/tasks-and-back-stack)."]]