[Android] TaskStackBuilder ๊ฐ„๋‹จ ์ •๋ฆฌ

[Android] TaskStackBuilder ๊ฐ„๋‹จ ์ •๋ฆฌ

Task์™€ Back Stack

  • Task: ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ์ž‘์—…์„ ํ•  ๋•Œ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” Activity ๋ชจ์Œ
    ๋ชจ๋“  Activity๊ฐ€ ์Šคํƒ์—์„œ ์ œ๊ฑฐ๋˜๋ฉด Task๋„ ์‚ฌ๋ผ์ง

๋ฉ€ํ‹ฐ ์œˆ๋„์šฐ ํ™˜๊ฒฝ์—์„œ ์•ฑ์ด ๋™์‹œ์— ์‹คํ–‰๋  ๋•Œ, ์‹œ์Šคํ…œ์€ ๊ฐ ์œˆ๋„์šฐ์˜ Task๋ฅผ ๋ณ„๋„๋กœ ๊ด€๋ฆฌํ•˜๋ฉฐ, ํ•˜๋‚˜์˜ ์œˆ๋„์šฐ์—๋Š” ์—ฌ๋Ÿฌ Task๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

  • Back Stack: Back Stack์— ๊ฐ Activity๊ฐ€ ์—ด๋ฆฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ.
    ํ˜„์žฌ Activity๊ฐ€ ๋‹ค๋ฅธ Activity๋ฅผ ์‹œ์ž‘ํ•˜๋ฉด, ์ƒˆ Activity๊ฐ€ ์Šคํƒ์˜ ๋งจ ์œ„์— push
    ์‚ฌ์šฉ์ž๊ฐ€ back ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด Activity๊ฐ€ ์ข…๋ฃŒ๋˜๊ณ  ์Šคํƒ์—์„œ pop

์Šคํƒ ๋‚ด Activity๋Š” ๋‹ค์‹œ ์ •๋ ฌ๋˜์ง€ ์•Š๊ณ  push์™€ pop ๋™์ž‘๋งŒ ํ•จ
-> Back Stack์€ LIFO(ํ›„์ž…์„ ์ถœ) ๊ตฌ์กฐ๋กœ ๋™์ž‘

์ƒ์„ธ ๋‚ด์šฉ์€ Android Developers์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

TaskStackBuilder

TaskStackBuilder๋Š” Task๊ฐ„ ์ด๋™ ์‹œ Back Stack ์กฐํ•ฉ์„ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์œ ํ‹ธ ํด๋ž˜์Šค

API 11(Android 3.0/Honeycomb)๋ถ€ํ„ฐ back ํ‚ค๋ฅผ ์ด์šฉํ•œ ์ด๋™ ๋™์ž‘์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.
Back ํ‚ค๋กœ๋Š” ํ˜„์žฌ Task ๋‚ด์—์„œ๋งŒ ๋™์ž‘ํ•˜๊ณ  ๋‹ค๋ฅธ Task๋กœ์˜ ์ด๋™์€ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.
๋‹ค๋ฅธ Task๋กœ์˜ ์ด๋™๊ณผ ์ด์ „ Task๋กœ์˜ ์ด๋™์€ โ€œ์ตœ๊ทผ ์•ฑโ€ UI๋ฅผ ํ†ตํ•ด ๊ฐ€๋Šฅํ•˜๋‹ค.

TaskStackBuilder ์‚ฌ์šฉ ์˜ˆ์‹œ - ์•Œ๋ฆผ PendingIntent

Notification์—์„œ Activity๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ, ์ƒˆ๋กœ์šด Back Stack์„ ๋งŒ๋“ค๋„๋ก TaskStackBuilder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ PendingIntent๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

1. Manifest์— Activity ๊ณ„์ธต ๊ตฌ์กฐ ์ •์˜

<activity> ์š”์†Œ์— android:parentActivityName ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ณ„์ธต ๊ตฌ์กฐ ์ •์˜

    <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>

2. Back Stack์ด ์žˆ๋Š” PendingIntent ์ž‘์„ฑ

TaskStackBuilder์˜ addNextIntentWithParentStack()์„ ์ด์šฉํ•˜์—ฌ ์‹œ์ž‘ํ•˜๋ ค๋Š” Activity์˜ Intent๋ฅผ ์ „๋‹ฌ
-> Manifest์— ์ •์˜ํ•œ๋Œ€๋กœ ์ „์ฒด Back Stack์ด ํฌํ•จ๋œ PendingIntent๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

    // 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)
    }
    

ํ•„์š”ํ•œ ๊ฒฝ์šฐ, editIntentAt()์„ ํ˜ธ์ถœํ•˜์—ฌ ์Šคํƒ ๋‚ด ํ•ด๋‹น index์˜ Intent๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
-> ์‚ฌ์šฉ์ž๊ฐ€ Back Stack์˜ Activity๊นŒ์ง€ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ, Back Stack ๋‚ด Activity์—์„œ ์˜๋ฏธ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

Comments