์•ฑ ๐Ÿ’ป/์•ˆ๋“œ๋กœ์ด๋“œ์ŠคํŠœ๋””์˜ค

์•ˆ๋“œ๋กœ์ด๋“œ ์™ธ๋ถ€ ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ์ฐธ์กฐํ•˜๊ธฐ - Kotlin

23.8 2022. 5. 3. 20:24
๋ฐ˜์‘ํ˜•

 

https://juahnpop.tistory.com/231

 

์•ˆ๋“œ๋กœ์ด๋“œ ์ฝ”ํ‹€๋ฆฐ : ์ผ๋ฐ˜ ํด๋ž˜์Šค ๋˜๋Š” Fragment์—์„œ MainActivity์˜ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

Android Kotlin : ์ผ๋ฐ˜ Class์—์„œ MainActivity์˜ ํ”„๋กœํผํ‹ฐ ๋ฐ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ ์ผ๋ฐ˜ Class ์™€ Fragment์—์„œ MainActivity์˜ ๋ฉ”์„œ๋“œ ๋ฐ ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ์— ๋Œ€ํ•ด ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. Fragment ์—์„œ MainActivity..

juahnpop.tistory.com

 

์ฐธ์กฐํ•˜๊ณ ์ž ํ•˜๋Š” class ๋‚ด์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธ

    init{
        instance = this
    }
    
    companion object{
    private var instance:MyActivity? = null
    fun getInstance(): MyActivity? {
    return instance
    }
}

 

์ดํ›„ ์™ธ๋ถ€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ํด๋ž˜์Šค์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธ ํ›„ ๋‚ด๋ถ€ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ๊ฐ€๋Šฅ

        private val myActivity = MyActivity.getInstance()
728x90
๋ฐ˜์‘ํ˜•