package com.yidianling.ydlcommon.custom.base

import android.support.v4.app.Fragment
import com.yidianling.ydlcommon.R

/**
 * 单个 Fragment 的 Activity，使用 Fragment 代替 Activity
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/01/27
 */
abstract class SingleFragmentActivity: BaseActivity() {

    override final fun layoutResId(): Int {
        return R.layout.activity_fragment
    }

    private val fm = supportFragmentManager

    override fun initDataAndEvent() {
        var fragment = fm.findFragmentById(R.id.fragment_container)
        if (fragment == null) {
            fragment = createFragment()
            fm.beginTransaction()
                    .add(R.id.fragment_container, fragment)
                    .commit()
        }
    }


    // 切换 fragment
    fun replace(fragment: Fragment) {

        fm.beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .addToBackStack(null)
                .commit()
    }

    protected abstract fun createFragment(): Fragment

}