โcontextโ์ ์ฌ์ ์ ์๋ฏธ๋ โ๋ฌธ๋งฅ, ํ๋ฆโ์ด๋ผ๋ ๋ป์ด๋ค.
๊ทธ ๋ป์ฒ๋ผ ์๋๋ก์ด๋์์๋ ์ดํ๋ฆฌ์ผ์ด์ ์ฆ, ๊ฐ์ฒด์ ํ์ฌ ํ๋ฆ์ ์ด์ผ๊ธฐํ๋ค.
context๋ ์ฝ๊ฒ ์ด์ผ๊ธฐํ๋ฉด ์ดํ๋ฆฌ์ผ์ด์ ์ โํ์ฌ ์ํโ๋ฅผ ๊ฐ์ง๊ณ ์๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
โก๏ธย Activity
,ย Application
ย ํด๋์ค๋ย Context
ย ํด๋์ค๋ฅผ ์์๋ฐ์ ํด๋์ค๋ค.
์ด๋ฐ Context
๋ 2๊ฐ๋ก ๋๋์ด์ง๋ค! Application Context
์ Activity Context
Activity
ย ์์ย ApplicationContext
ย ๋ผ๋ ํ๋กํผํฐ๋ฅผ ํตํด ์ป์ ์ ์๋ ์ฑ๊ธํค ์ธ์คํด์ค์ด๋ค.
๐ย ๋ด๊ฐ ๋ชฐ๋ผ์ ์ค๋นํ ์๋๋ก์ด๋ ์ฌ์ ๐
[์ฑ๊ธํค ์ธ์คํด์ค]: ์์ฑ์๊ฐ ์ฌ๋ฌ ์ฐจ๋ก ํธ์ถ๋๋๋ผ๋ ์ค์ ๋ก ์์ฑ๋๋ ๊ฐ์ฒด๋ ํ๋์ด๊ณ ์ต์ด ์์ฑ ์ดํ์ ํธ์ถ๋ ์์ฑ์๋ ์ต์ด์ ์์ฑ์๊ฐ ์์ฑํ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ๋์์ธ ํจํด
ํ๋ก๊ทธ๋จ ๋ด์์ ํ๋์ ๊ฐ์ฒด๋ง ์กด์ฌํด์ผํ๊ณ , ๊ทธ ๊ฐ์ฒด๋ฅผ ํ๋ก๊ทธ๋จ ๋ด์์ ์ฌ๋ฌ ๋ถ๋ถ์์ ๊ณต์ ํด์ ์ฌ์ฉํ ๋ ์ฌ์ฉํ๋ค.
์ดํ๋ฆฌ์ผ์ด์
์ ์๋ช
์ฃผ๊ธฐ์ ๋ฌถ์ฌ์๋ค๋ ํน์ง์ด ์๋ค. ๊ทธ๋์โย Context
๊ฐ ์ข
๋ฃ๋๊ณ ๋์๋ ํ์ํ ์์
์ด๋,
์กํฐ๋นํฐ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ๊ณณ์ Context
๊ฐ ํ์ํ ์์
์ ์ ํฉํ๋ค!
์์ ApplicationContext
์ ์ ๋๋ก ์ดํดํ๋ค๋ฉด ์์๋ฑ๊น์ผ์ผ๋ก ActivityContext
๋ ๋~์ถฉ ์ด๋ฐ ๋ด์ฉ์ด๊ฒ ๋ค ์ถ์ฃ ?
(๋ค. ์ ๊ฐ ๊ทธ๋ฌ์ต๋๋ค.)
ActivityContext
๋ Activity์ ์๋ช
์ฃผ๊ธฐ๊ฐ ๋ฌถ์ฌ์๋ค. ๊ทธ๋์ Activity ์์์๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
Activity
ย ์ค์ฝํ ๋ด์์ ์ฌ์ฉ๋ ๋ ๋๊ฒจ์ค๋คActivity
ย ์ย ์๋ช
์ฃผ๊ธฐ๊ฐ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ๋๊ฒจ์ค๋ค.โ ์ฆ,ย Activity
ย ๊ฐ ์๋ฉธ๋๋ฉด ํด๋นย Context
ย ๋ ๊ฐ์ด ์๋ฉธ๋๋ ๊ฒ์ด๋ค.
๋์ ์ดํ๋ฆฌ์ผ์ด์ ์์๋ 2๊ฐ์ Activity๊ฐ ์๋ค. ์ฌ๊ธฐ์ Context์ฌ์ฉ์ ์ด๋ ๊ฒ ๊ฐ๋ฅํ๋ค.
ApplicationContext
๋ 2๊ฐ์ Activity ๋ชจ๋์์ ์ฌ์ฉ๊ฐ๋ฅํ๋ค.Activity1Context
๋ MainActivity1์์๋ง ์ฌ์ฉ๊ฐ๋ฅํ๋ค.Activity2Context
๋ MainActivity2์์๋ง ์ฌ์ฉ๊ฐ๋ฅํ๋ค.๊ทธ๋ฌ๋ฉด ์ฌ๊ธฐ์ ์ด๋ฐ ์๊ฐ์ ํด๋ณผ ์ ์๋ค!
โ๊ทธ๋ฅ ๋ชจ๋ ๊ณณ์์ ApplicationContext
๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ ๊ฑฐ ์๋์ผ?โ
โผ๏ธย ์๋ชป๋ Context
์ฌ์ฉ์ ๋ฉ๋ชจ๋ฆฌ ๋ฆญ ๋ฌธ์ ๋ก ์ด์ด์ง ์ ์๊ธฐ ๋๋ฌธ์ ์ฃผ์ํด์ ์ฌ์ฉ ํ ๊ฒ โผ๏ธ
๐ย ๋ด๊ฐ ๋ชฐ๋ผ์ ์ค๋นํ ์๋๋ก์ด๋ ์ฌ์ ๐
[๋ฉ๋ชจ๋ฆฌ ๋ฆญ]: ์ฑ์ ๋ฉ๋ชจ๋ฆฌ ์์์ ๋์๊ฐ๊ณ , ๊ทธ ๊ณต๊ฐ์ ํ์ ๋์ด์๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์ด๋ค ๊ฐ์ฒด๊ฐ ๋ ์ด์ ์ฌ์ฉ๋์ง ์๋๋ค๊ณ ํ๋จํ๋ฉด ๋ฉ๋ชจ๋ฆฌ์์ ๊ฐ๋น์ง ์ปฌ๋ ํฐ์ ์ํด์ ์ ๊ฑฐ๊ฐ ๋์ด์ผ ํ๋ค.
ํ์ง๋ง ์์ ์ฌ์ง๊ณผ ๊ฐ์ด ์ฌ์ฉํ์ง ์๋ ๊ฐ์ฒด ์ค ์ฐธ์กฐ๋๋ ๊ฐ์ฒด๊ฐ ์๊ธฐ๊ณ , ๊ทธ๋์ ๊ฐ๋น์ง ์ปฌ๋ ํฐ๊ฐ ์ด๋ฐ ๊ฒ๋ค์ ์ ๊ฑฐํด์ผ ๋๋ ๋์์ผ๋ก ์๊ฐํ์ง ์๋๋ค. ์ด์ ๊ฐ์ด ๊ฐ์ฒด์ ํ ๋น๋์ด ์๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐพ์์ฌ ์ ์๋ ๊ฒฝ์ฐ์ โ๋ฉ๋ชจ๋ฆฌ ๋ฆญโ, โ๋ฉ๋ชจ๋ฆฌ ๋์โ๊ฐ ๋ฐ์ํ๋ค.
Application ์ ์ฑ ํ๋ก์ธ์ค๊ฐ ์ด์์๋ ๋์ ๊ณ์ํ์ฌ ๋จ์์๋ค. ๋ฐ๋ผ์,ย Application Contextย ๋ฅผ ํ์ฉํ ๊ฐ์ฒด๋ฅผย ๋ฉ๋ชจ๋ฆฌ์์ ํ ๋น ํด์ ํ์ง ์๊ณ ย ์์ ๊ฒฝ์ฐ,ย ๋ฉ๋ชจ๋ฆฌ ๋ฆญ์ด ๋ฐ์ ํ ํ๋ฅ ์ด ์ฌ๋ผ๊ฐ๋ค.
์ ์๋ฏธ๋ binding.root
์ context
ํ๋ค๋ ๋ป์ด๊ฒ ์ฃ ?
binding.root
๋ ํด๋น ViewBinding์ด ๋ฐ์ธ๋ฉํ๋ ๋ ์ด์์์ ์ต์์ ๋ทฐ๋ฅผ ์๋ฏธํ๋ค.
โclass MyActivity : AppCompatActivity() {
private lateinit var binding: MyActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ViewBinding ์ด๊ธฐํ
binding = MyActivityBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// binding.root.context๋ฅผ ํตํด ํ์ฌ ์กํฐ๋นํฐ์ ์ปจํ
์คํธ์ ์ ๊ทผ
val currentContext = binding.root.context
// currentContext๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ํ ์์
์ํ
}
}
binding.root.context
๋ฅผ ํตํด ์ต์์ ๋ทฐ์ ์ปจํ
์คํธ์ ์ ๊ทผํ๋ ๊ฒ์ ํด๋น UI ๊ตฌ์ฑ ์์๊ฐ ์ํ ํ๋์ด๋ ํ๋๊ทธ๋จผํธ์ ๊ด๋ จ๋ ์ปจํ
์คํธ์ ์ง์ ์ ์ผ๋ก ์ ๊ทผํ ์ ์๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
๋ฆฌ์์ค ์ก์ธ์ค ex) ๋ฌธ์์ด, ์์, ๊ทธ๋ฆผ ๋ฑ์ ๋ฆฌ์์ค
val stringResource = binding.root.context.getString(R.string.my_string_resource)
val colorResource = ContextCompat.getColor(binding.root.context, R.color.my_color_resource)
์์คํ ์๋น์ค ์์ธ์ค ex) ์๋ฆผ ์๋น์ค, ํค๋ณด๋ ์๋น์ค ๋ฑ
val notificationManager = binding.root.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
ํ ์คํธ ๋ฉ์ธ์ง ํ์
Toast.makeText(binding.root.context, "Hello, World!", Toast.LENGTH_SHORT).show()
์ํฐ๋นํฐ ์ ํ
val intent = Intent(binding.root.context, AnotherActivity::class.java)
binding.root.context.startActivity(intent)
ํ ๋ง ๋ฐ ์คํ์ผ ์ ์ฉ
val themedContext = ContextThemeWrapper(binding.root.context, R.style.MyCustomTheme)
โ ์ด ์์
๋ค์ ์กํฐ๋นํฐ ์์์ ์ด๋ค์ง๋ ๊ฒ๋ค์ด๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๋ ActivityContext
์ ์ ๊ทผํด์ผํ๋ค.
๊ทธ๋ผ context์ ์ ๊ทผํ ์ ์๋ ๋ฉ์๋๋ค์ ์์๋ณด์.
getApplicationContext()
getApplication()
getBaseContext()
getContext()
getContext()
requireContext()
context ์ฌ์ฉ์ ์ด๋ ค์ด ์ ์ ๋ฉ์๋๋ค ๊ฐ ์ด๋ค ์ฐจ์ด๊ฐ ์๋์ง, ์ด๋์ ์ฌ์ฉํด์ผ ํ๋์ง ๊ตฌ๋ณํ๊ธฐ ํ๋ค๋ค๋ ์ ์ด๋ค.
ํ์ง๋ง ํด๋น ๋ฉ์๋๋ค์ด ์ด๋ค ํด๋์ค์์ ์ฌ์ฉ๋๋์ง ๋ณด๋ฉด ํํธ๋ฅผ ์ป์ ์ ์๋ค
ContextWrapper.getApplicationContext()
Activity.getApplication()
Service.getApplication()
ContextWrapper.getBaseContext()
View.getContext()
Fragment.getContext()
Fragment.requireContext()
์ด๊ฑธ ์ ๋๋ก ์ดํดํ๊ธฐ ์ํด์ Context ํด๋์ค์ ๊ตฌ์กฐ๋ถํฐ ์์๋ณด์.
Context
: Contextํด๋์ค์ ์ต์๋จ์ ์์นํ๋ฉฐ, ์ถ์ ํด๋์ค๋ก ๋ด๋ถ ๊ตฌํ์ด ๊ฑฐ์ ์๊ณ , ์์ ์ ์๋ค๊ณผ ์ถ์ ๋ฉ์๋๋ค๋ก ์ด๋ฃจ์ด์ง ํด๋์ค์ด๋ค.public abstract class Context
Contextlmpl
: ์์ ํด๋์ค์ธ Context์ ๊ตฌํ์ด ์ด๋ฃจ์ด์ง๋ ํด๋์ค์ด๊ณ , Context๋ฅผ ์์๋ฐ์ ๊ตฌํํ๋ค.โ ํ์ง๋ง Application, Activity, Service๊ฐ Contextlmpl์ ์ง์ ์ฌ์ฉํ์ง ์๋๋ค.
ContextWrapper
: Contextlmpl์ ๋ฉํํ๊ณ ์๋ ํด๋์ค์ด๋ค. ๐ย ๋ด๊ฐ ๋ชฐ๋ผ์ ์ค๋นํ ์๋๋ก์ด๋ ์ฌ์ ๐
[๋ฉํ]: ๊ธฐ์กด์ ํด๋์ค๋ ๊ธฐ๋ฅ์ ๋ ๊ฐ์ธ๊ฑฐ๋ ํ์ฅํ์ฌ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐ or ๋ณ๊ฒฝํ๋ ํ๋ก์ธ์ค๋ฅผ ๋ปํ๋ค. (๋ฉํ์ ๊ฐ๋ ์ด ์ ์ดํด๊ฐ ๋์ง ์๋๋ค๋ฉด Context์ ๊ตฌ์กฐ์ ๋ํด์ ๋จผ์ ์ดํดํ๋ ๊ฒ๋ Not Bad~)
import android.content.Context
import android.content.ContextWrapper
import android.content.SharedPreferences
class MyContextWrapper(base: Context) : ContextWrapper(base) {
companion object {
private const val PREFERENCES_NAME = "MyPreferences"
private const val KEY_USERNAME = "username"
}
// ์ฌ์ฉ์ ์ด๋ฆ์ ์ ์ฅํ๋ ๋ฉ์๋
fun saveUsername(username: String) {
val sharedPreferences = getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString(KEY_USERNAME, username)
editor.apply()
}
// ์ฌ์ฉ์ ์ด๋ฆ์ ๋ถ๋ฌ์ค๋ ๋ฉ์๋
fun loadUsername(): String? {
val sharedPreferences = getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE)
return sharedPreferences.getString(KEY_USERNAME, null)
}
}
์์ ์ฝ๋์์ ContextWrapper
์์ฑ์๋ฅผ ๋ณด๋ฉด base
๋ผ๋ Context
๋ฅผ ๋ฐ๊ณ ์๋ค.
์ด base๊ฐ Context ๊ตฌํ์ ํด๋นํ๋ค.
MyContextWrapper
๋ base
์ ์ ๋ฌ๋ Context
๋ฅผ ๊ฐ์ธ๊ณ , ๊ทธ ์์ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ฌ ์ฌ์ฉ์ ์ด๋ฆ์ ์ ์ฅํ๊ณ ๋ถ๋ฌ์ค๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ ์ญํ ์ ํ๋ค.
์ด๋ ๊ฒ ํจ์ผ๋ก์จ, MyContextWrapper
ํด๋์ค๋ ๊ธฐ์กด Context
์ ๊ธฐ๋ฅ์ ๊ทธ๋๋ก ์ฌ์ฉํ๋ฉด์ ํ์ํ ์ถ๊ฐ ๊ธฐ๋ฅ์ ๋ฉํํ๊ณ ํ์ฅํ ์ ์๋ค.
์ด๋ฅผ ํตํด ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ค์ํ ๋ถ๋ถ์์ ์ด ํด๋์ค๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ ํน์ ๋์์ ์ผ๊ด๋๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค.
Application
, Service
, Activity
: ContextWrapper์ ์์๋ฐ์ ์ฐ๋ฆฌ๊ฐ Context๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.์ด๋ ๊ฒ Context์ ๊ตฌ์กฐ์ ๋ํด์ ์์๋ณด๊ณ ๋ค์ ๋ฉ์๋๋ค์ ๋ด๋ณด์.
ContextWrapper.getApplicationContext()
ContextWrapper์ ๋ฉ์๋์ด๋ฉฐ, ์ฑ๊ธํค ์ดํ๋ฆฌ์ผ์ด์
๊ฐ์ฒด์ context๋ฅผ ๋ฆฌํด
Activity.getApplication()
Activity์ ๋ฉ์๋์ด๋ฉฐ, Activity๋ฅผ ์์ ํ๋ ์ดํ๋ฆฌ์ผ์ด์
๊ฐ์ฒด๋ฅผ ๋ฆฌํด
Service.getApplication()
Service์ ๋ฉ์๋์ด๋ฉฐ, Service๋ฅผ ์์ ํ๋ ์ดํ๋ฆฌ์ผ์ด์
๊ฐ์ฒด๋ฅผ ๋ฆฌํด
ContextWrapper.getBaseContext()
ContextWrapper์ ๋ฉ์๋์ด๋ฉฐ, ์์ฑ์ ๋๋ setBaseContext()๋ฅผ ํตํด ์ด๊ธฐํ๋ base context ๋ฆฌํด
(์๊น ContextWrapper ํด๋์ค๋ฅผ ์์๋ณด๋ฉด์ ๋ดค๋ ๋ฉ์๋์
๋๋ค!)
View.getContext()
View๊ฐ ์ฌ๋ผ๊ฐ์๋ ์ปจํ
์คํธ๋ฅผ ๋ฆฌํด
Fragment.getContext()
Fragment๊ฐ ์ฐ๊ฒฐ๋ ActivityContext๋ฅผ ๋ฆฌํด(Nullable)
Fragment.requireContext()
Fragment๊ฐ ์ฐ๊ฒฐ๋ ActivityContext๋ฅผ ๋ฆฌํด(NonNull)