๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๋’ค๋กœ๊ฐ€๊ธฐ ๋ฒ„ํŠผ ์„ค์ •ํ•˜๊ธฐ(getOnBackPressedDispatcher) ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๋’ค๋กœ๊ฐ€๊ธฐ ๋ฒ„ํŠผ ์„ค์ •ํ•˜๊ธฐ(getOnBackPressedDispatcher)

๋ฃจ๋ฆฌ์•ผใ…‘ 2023. 12. 27. 16:16
๋ฐ˜์‘ํ˜•

์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์—๋Š” ๋’ค๋กœ๊ฐ€๊ธฐ ๋ฒ„ํŠผ์ด ์žˆ๋‹ค.

Back ๋ฒ„ํŠผ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

 

getOnBackPressedDispatcher

onCreate ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ž‘์„ฑํ•œ๋‹ค.

        // Back ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜

        getOnBackPressedDispatcher().addCallback(new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                // ๋ฐฑ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ ํ•˜๊ณ  ์‹ถ์€ ์ฝ”๋“œ ์ž‘์„ฑ.

                // ๋ฉ”์ธ Activity๋กœ, ๋‚˜์ด +10 ๊ฐ’ ์ถœ๋ ฅ

                age = age+10;

                Intent intent = new Intent();
                intent.putExtra("age",age);
                setResult(100,intent);

                finish();

            }
        });

 

๋ฐ˜์‘ํ˜•