[android/Kotlin] fragment 에서 thread/coroutine 중첩 적용 에러

love&peace·2023년 9월 30일

이슈: thread/coroutine이 framgment를 바꿀때 마다 thread가 생성 되어서 배너의 자동 전환이 빨라졌다.


해결: onViewCreated에서 coroutine을 생성하지 말고 onAttach에서 생성한다

override fun onAttach(context: Context) {
        super.onAttach(context)
        CoroutineScope(Dispatchers.Main).launch {
            while (bannerLoop) {
                if (binding.homaBannerViewPager.currentItem == 3) {
                    delay(3000)
                    binding.homaBannerViewPager.currentItem = 0
                    continue

                }
                delay(3000)
                binding.homaBannerViewPager.setCurrentItem(
                    binding.homaBannerViewPager.currentItem,
                    true
                )
                binding.homaBannerViewPager.currentItem += 1
            }
        }
    }

0개의 댓글