수업

모바일프로그래밍 - lifeCycle (Dynamic Fragment)

eunslog 2023. 3. 28. 10:39

FragmentContainer를 갖다 넣음.

 

오류가 뜨면 위에 Infer Constraints 넣음.

 

companion object {
    fun newInstance() = LifecycleFragment()
}

class 객체 하나만 만들어줌.

 

 

 

viewBinding 하기 위해 build.gradle에 위 문장 넣기.

 

 

 

 

 

MainActivity.kt

package com.example.dynamicfragment

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.content.PackageManagerCompat.LOG_TAG

class MainActivity : AppCompatActivity() {
    val LOG_TAG :String = "***********"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.e(LOG_TAG, "MainActivity: onCreate() called")

        supportFragmentManager.beginTransaction()
            .add(R.id.contentFrame, LifecycleFragment.newInstance(), "testfragment" )
            .commit()
    }
}

add -> 전체 화면에 lifecycleFragment 를 갖다붙인 것.

 

 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/contentFrame"
        android:name="com.example.dynamicfragment.LifecycleFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="354dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

LifecycleFragment.kt

package com.example.dynamicfragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.dynamicfragment.databinding.FragmentLifecycleBinding

class LifecycleFragment : Fragment() {
    var LOG_TAG = "$$$ Fragment $$$"
    private lateinit var binding: FragmentLifecycleBinding //fragment_lifecycle.xml

    companion object {
        fun newInstance() = LifecycleFragment()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(LOG_TAG, "LifecycleFragment: onCreate() called")
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d(LOG_TAG, "LifecycleFragment: onCreateView() called")

        binding = FragmentLifecycleBinding.inflate(inflater, container, false)
        binding.txtText.text = "From Fragment!!!"
        return binding.root
    }

    override fun onStart() {
        Log.d(LOG_TAG, "LifecycleFragment: onStart() called")
        super.onStart()
    }

    override fun onResume() {
        Log.d(LOG_TAG, "LifecycleFragment: onResume() called")
        super.onResume()
    }

    override fun onPause() {
        Log.d(LOG_TAG, "LifecycleFragment: onPause() called")
        super.onPause()
    }

    override fun onStop() {
        Log.d(LOG_TAG, "LifecycleFragment: onStop() called")
        super.onStop()
    }

    override fun onDestroy() {
        Log.d(LOG_TAG, "LifecycleFragment: onDestroy() called")
        super.onDestroy()
    }

    override fun onDestroyView() {
        Log.d(LOG_TAG, "LifecycleFragment: onDestroyView() called")
        super.onDestroyView()
    }
}

 

 

fragment_lifecycle.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LifecycleFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/txtText"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="@string/hello_blank_fragment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

정상 동작함.