본문 바로가기
Android Studio

[Android, Kotlin] Notification Click시 실행 중인 App 띄우기(intent Setting)

by 열정적인 이찬형 2022. 10. 3.

타이머를 진행중에 Notification을 띄운 후 타이머가 종료되면 없어지도록 하였습니다.

 

하지만 타이머가 진행중에 BackGround에 있을 때 PendingIntent를 이용해서 다시 돌아오도록 하였지만 돌아오는 Activity가 새롭게 실행되는 것을 확인하였습니다.

 

제가 원하는 것은 현재 BackGround에서 실행 중인 타이머 그대로 돌아오는 것을 원하였습니다.

 

이 문제에 대해 해결한 방법을 정리하였습니다.


Intent

Android Develops에서 Intent를 살펴보면

 

Intent  |  Android Developers

android.net.wifi.hotspot2.omadm

developer.android.com

 

 

Task에서는 Activity는 Stack(LIFO)에 형태로 보관 및 관리합니다.

 

Action과 Category를 설정하여 어떤 형태로 Intent를 진행할 것인지 설정이 가능합니다

Intent에서 Flag를 설정하여 Task 내 Activity의 흐름을 제어가 가능합니다.

 

요약.

 

Notification을 이용하여 PendingIntent를 사용할 때 Intent와 같이 사용합니다.

Intent를 설정할 때 원래의 형태에 Task Stack으로 App이 다시 진행되도록 설정합니다.


사용법

 

Notification을 띄우는 Activity

    //CountDown Timer Start Notification
    private fun setNotification(){
        //intent 설정
        val intent = Intent(requireContext(), DetailActivity::class.java).apply {
            action = Intent.ACTION_MAIN
            addCategory(Intent.CATEGORY_LAUNCHER)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
        }
        val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
            PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_MUTABLE)
        else
            PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        //notification 설정
        ....
    }

Android DevelopsIntent에 나오는 설명을 살펴보면

 

action : Intent.ACTION_MAIN 

 

Start as a main entry point, does not expect to receive data.

(주 진입점으로 시작하여 데이터를 수신하지 않습니다.)

 

 

category : Intent.CATEGORY_LAUNCHER

 

Should be displayed in the top-level launcher.

(최상위 런처에 표시되도록 합니다.)

 

flags : Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP

 

Intent.FLAG_ACTIVITY_NEW_TASK

 

If set, this activity will become the start of a new task on this history stack.

(이 활동이 설정된 경우 이 기록 스택에 대한 새 작업의 시작이 됩니다.)

 

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started

(이 Flag를 사용할 때 현재 시작 중인 활동에 대해 작업이 이미 실행 중인 경우 새 활동이 시작되지 않습니다.)

 

Intent.FLAG_ACTIVITY_SINGLE_TOP

 

If set, the activity will not be launched if it is already running at the top of the history stack.

(설정된 경우 활동이 이미 기록 스택의 맨 위에서 실행 중인 경우 작업이 시작되지 않습니다.)

 

 

작업 및 백 스택 이해  |  Android 개발자  |  Android Developers

일반적으로 앱에는 여러 활동이 포함됩니다. 각 활동은 사용자가 실행할 수 있는 특정 종류의 작업을 중심으로 설계되어야 하며 다른 활동을 시작할 수 있어야 합니다. 예를 들어 이메일 앱에는

developer.android.com

 

위에서 살펴보면 singleTop에 대한 내용을 살펴보면

 

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

(활동 인스턴스가 현재 작업의 맨 위에 이미 있는 경우 시스템은 활동의 새 인스턴스를 만드는 대신 onNewIntent() 메서드에 대한 호출을 통해 의도를 해당 인스턴스로 라우팅합니다.)

 

요약.

 

Intent.ACTION_MAIN :  해당 Activity로 이동합니다.

 

Intent.CATEGORY_LAUNCHER : 최상위 View로 설정되도록 합니다.

 

Intent.FLAG_ACTIVITY_NEW_TASK : Activity를 새작업으로 진행하지만 기존에 존재하는 활동중일 때 새 활동은 시작하지 않습니다.

 

Intent.FLAG_ACTIVITY_SINGLE_TOP : 실행하려는 Activity가 Task Stack에 최상단에 있을 경우 기존 Activity를 불러오도록 진행합니다.

 

Intent에 Intent.FLAG_ACTIVITY_NEW_TASK와 Intent.FLAG_ACTIVITY_SINGLE_TOP를 설정하여서 기존에 있었던 Activity를 실행하도록 한 것입니다.

 

더 좋은 방법이 존재한다면 댓글로 남겨주시면 감사하겠습니다.

댓글