package com.cheng.blzb.ui.activity

import android.content.Context
import android.content.Intent
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import com.example.base.ui.BaseActivity


/**
 * 所有的普通的fragment页面启动都用此activity装载
 */
class PublicActivity : BaseActivity() {
    private var mFragment: Fragment? = null

    val attachedFragment: Fragment?
        get() = mFragment

    override fun getFragment(): Fragment {
        val className = intent.getStringExtra(FRAGMENT_CLASS_NAME)!!
        val clazz = Class.forName(className)

        mFragment = clazz.getDeclaredConstructor().newInstance() as Fragment
        mFragment!!.arguments = intent.extras
        return mFragment!!
    }

    companion object {
        const val FRAGMENT_CLASS_NAME = "fragment_class_name"

        fun start(context: Context?, clazz: Class<out Fragment>, vararg args: Pair<String, *>) {
            val intent = Intent(context, PublicActivity::class.java)
            intent.putExtra(FRAGMENT_CLASS_NAME, clazz.name)
            intent.putExtras(bundleOf(*args))

            context?.startActivity(intent)
        }

        fun newStart(context: Context?, clazz: Class<out Fragment>, vararg args: Pair<String, *>) {
            val intent = Intent(context, PublicActivity::class.java)
            intent.putExtra(FRAGMENT_CLASS_NAME, clazz.name)
            intent.putExtras(bundleOf(*args))
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context?.startActivity(intent)
        }
    }
}
