package com.example.criminalintent

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
/*
A class we invented specifically for an activity containing
exactly one fragment.

An "abstract" class is a class containing methods whose code has
yet to be written.

If a class is abstract, it must be inherited, and code supplied to the
missing methods before it can be used.
 */
abstract class SingleFragmentActivity : FragmentActivity() {

    protected abstract fun createFragment():Fragment
    /* createFragment is a method belonging to SingleFragmentActivity,
    but no code has been written for it
     */

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d ("SINGLEFRAGMENTACTIVITY","onCreate")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fragment)
//the fragment manager is what you use if your activity is going to use fragments.
        val fm: FragmentManager? = supportFragmentManager
        var fragment: Fragment? = fm!!.findFragmentById(R.id.fragment_container)
        if (fragment==null) { //creating fragment if it doesn't already exist.
            fragment = createFragment()
            fm.beginTransaction().add(R.id.fragment_container,fragment).commit()
        }
    }

    override fun onStart() {
        Log.d ("SINGLEFRAGMENTACTIVITY","onStart")
        super.onStart()
    }
    override fun onRestart() {
        Log.d ("SINGLEFRAGMENTACTIVITY","onRestart")
        super.onRestart()
    }
    override fun onPause() {
        Log.d ("SINGLEFRAGMENTACTIVITY","onPause")
        super.onPause()
    }
    override fun onResume() {
        Log.d ("SINGLEFRAGMENTACTIVITY","onResume")
        super.onResume()
    }
    override fun onDestroy() {
        Log.d ("SINGLEFRAGMENTACTIVITY","onDestroy")
        super.onDestroy()
    }
    override fun onStop() {
        Log.d ("SINGLEFRAGMENTACTIVITY","onStop")
        super.onStop()
    }
}

/*
A fragment is a portion of an activity screen.  The fragment is laid out
like an activity, but it (usually) doesn't take up the full screen.
You can have more than one fragment on a screen at any given time.


 */