[Android] Dialog vs DialogFragment

๐Ÿ’ก์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค ์ธํ„ฐ๋ทฐ์—์„œ Dialog์™€ DialogFragment์˜ ์ฐจ์ด์— ๋Œ€ํ•œ ์งˆ๋ฌธ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•ด ํ•™์Šตํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๊ฐœ์š”

  • ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๊ธฐ ์œ„ํ•ด ํŒ์—… UI๋กœ Dialog์™€ DialogFragment๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋‘ ๊ฐ€์ง€ ๋ชจ๋‘ ๋Œ€ํ™”์ƒ์ž๋ฅผ ๋„์šฐ๋Š”๋ฐ ์‚ฌ์šฉ๋˜์ง€๋งŒ, ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ ์ธก๋ฉด์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

Dialog

  • Dialog๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์‚ฌ์šฉ์ž์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ํŒ์—… UI๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
  • ์ž„์‹œ ์ž‘์—…์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ์š”์ฒญํ•˜๊ฑฐ๋‚˜ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • AlertDialog, DatePickerDialog, ProgressDialog์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

ํŠน์ง•

  • Activity์— ์†ํ•œ ํŒ์—… ์ฐฝ์œผ๋กœ, ๋ณต์žกํ•œ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค๊ธฐ๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํ™”๋ฉด ํšŒ์ „์ด๋‚˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ,ํฌ๊ทธ๋ผ์šด๋“œ ์ „ํ™˜ ์‹œ์— ์ƒํƒœ ์œ ์ง€๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
  • ๋…๋ฆฝ์ ์ธ ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ์—†์œผ๋ฉฐ, ์•กํ‹ฐ๋น„ํ‹ฐ์— ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.
val dialog = AlertDialog.Builder(this)
    .setTitle("Dialog Example")
    .setMessage("This is a simple dialog.")
    .setPositiveButton("OK") { _, _ ->
        // Positive button clicked
    }
    .setNegativeButton("Cancel", null)
    .create()

dialog.show()

DialogFragment

  • DialogFragment๋Š” ๋Œ€ํ™” ์ƒ์ž๋ฅผ Fragment๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
  • ์•ˆ๋“œ๋กœ์ด๋“œ API 11(Android 3.0)๋ถ€ํ„ฐ ๋„์ž…๋˜์—ˆ์œผ๋ฉฐ, ์ƒ๋ช…์ฃผ๊ธฐ์™€ ํ•จ๊ป˜ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ํ™œ์šฉํ•ด์„œ ๋…๋ฆฝ์ ์ธ ๋Œ€ํ™”์ƒ์ž๋ฅผ ๋งŒ๋“ค๊ณ , ๋‹ค๋ฅธ ํ”„๋ž˜๊ทธ๋จผํŠธ์™€ ํ†ตํ•ฉํ•˜๊ฑฐ๋‚˜ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

DialogFragment  |  Android Developers

 

DialogFragment  |  Android Developers

 

developer.android.com

ํŠน์ง•

  • FragmentManager๋ฅผ ํ†ตํ•ด Fragment์™€ ํ†ตํ•ฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ด€๋ฆฌ๋„ ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค.
  • FragmentTransaction์„ ํ†ตํ•ด BackStack์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์–ด, ์‚ฌ์šฉ์ž ๊ฒฝํ—˜ ์ธก๋ฉด์—์„œ ๋” ์œ ์—ฐํ•œ ํ๋ฆ„์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋‚ด๋ถ€์ ์œผ๋กœ Dialog๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์–ด์„œ Dialog์˜ ์žฅ์ ์„ ๋ชจ๋‘ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
class MyDialogFragment : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return AlertDialog.Builder(requireContext())
            .setTitle("DialogFragment Example")
            .setMessage("This is a dialog from DialogFragment.")
            .setPositiveButton("OK") { _, _ ->
                // Positive button clicked
            }
            .setNegativeButton("Cancel", null)
            .create()
    }
}

// Fragment or Activity์—์„œ ํ˜ธ์ถœ
val dialogFragment = MyDialogFragment()
dialogFragment.show(supportFragmentManager, "dialog")

์ •๋ฆฌ

  • ๊ณต์‹๋ฌธ์„œ์—์„œ๋Š” ์ƒ๋ช…์ฃผ๊ธฐ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•œ DialogFragment ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ๊ธฐ์กด Dialog๋ฅผ ํ™œ์šฉํ•  ๋•Œ Activity๊ฐ€ ํŒŒ๊ดด๋˜๋”๋ผ๋„ Dialog๊ฐ€ ์กด์žฌํ•˜์—ฌ, Leaked Window ๋ฌธ์ œ๋‚˜ IllegalStateException ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ 

https://bb-library.tistory.com/258