package com.idle.wall.idleactivity

import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity

open class BaseActivity: AppCompatActivity() {
        protected var backPressedCallback: OnBackPressedCallback? = null
        /** 子类是否需要拦截返回 */
        protected open fun shouldInterceptBackPress(): Boolean = false

        /** 子类定义拦截后的操作（例如弹窗） */
        protected open fun onInterceptBackPressed() {}

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setupBackPressedCallback()//初始化back事件
        }
        private fun setupBackPressedCallback() {
            backPressedCallback = object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    if (shouldInterceptBackPress()) {
                        // 由子类处理拦截动作
                        onInterceptBackPressed()
                    } else {
                        // 不拦截：关闭自己
                        isEnabled = false
                        onBackPressedDispatcher.onBackPressed()
                    }
                }
            }

            onBackPressedDispatcher.addCallback(this, backPressedCallback!!)

        }
}