liangjingkanji / Tooltip

🍞 Android 快速创建吐司/对话框 Quick crete Toast or Dialog
https://liangjingkanji.github.io/Tooltip/
MIT License
119 stars 9 forks source link

可以考虑完善一下,让 BubbleDialog 具备生命周期感知 #8

Open syxc opened 7 months ago

syxc commented 7 months ago

加了几句代码,以防部分情况下使用不当造成内存泄露,仅供参考。

private val mContext = context

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.layout_bubble_dialog)

    tvTitle = findViewById(R.id.tv_title)
    tvTitle?.text = title
    val rotateDrawable = findViewById<ImageView>(R.id.iv_loading).background as RotateDrawable
    ObjectAnimator.ofInt(rotateDrawable, "level", 0, 10000).apply {
        duration = 2000
        repeatCount = ValueAnimator.INFINITE
        interpolator = LinearInterpolator()
        start()
    }

    (mContext as? ComponentActivity)?.lifecycle?.addObserver(
        object : LifecycleObserver {
            @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
            fun onStop() {
                if (isShowing) {
                    dismiss()
                    Log.d("BubbleDialog", "---onStop dismiss---")
                }
            }

            @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
            fun onDestroy() {
                if (isShowing) {
                    dismiss()
                    Log.d("BubbleDialog", "---onDestroy dismiss---")
                }
            }
        },
    )
}