package com.bearya.kids.main

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.navigation.Navigation
import com.bearya.kids.R
import com.bearya.kids.databinding.ActivityMainBinding
import com.orhanobut.logger.Logger

class MainActivity : AppCompatActivity() {

    private lateinit var bindView: ActivityMainBinding

    private val fragmentLifecycleCallback = object : FragmentManager.FragmentLifecycleCallbacks() {

        override fun onFragmentCreated(fm: FragmentManager, f: Fragment, savedInstanceState: Bundle?) {
            if ("SupportRequestManagerFragment" != f.javaClass.simpleName) {
                Logger.t("Current").v("================= ${f.javaClass.simpleName} onFragmentCreated =================")
            }
        }

        override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) {
            if ("SupportRequestManagerFragment" != f.javaClass.simpleName) {
                Logger.t("Current").v("================= ${f.javaClass.simpleName} onFragmentDestroyed =================")
            }
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindView = DataBindingUtil.setContentView(this, R.layout.activity_main)
        Logger.t("Current").v("================= MainActivity onCreate =================")
        // recursive : 是否递归Fragment中的Fragment，就是Fragment栈中的Fragment
        supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallback, true)
    }

    override fun onSupportNavigateUp(): Boolean = Navigation.findNavController(this, R.id.container_fragment).navigateUp()

    override fun onDestroy() {
        super.onDestroy()
        supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallback)
        Logger.t("Current").v("================= MainActivity onDestroyed =================")
    }

}