package com.spark.android.ui.base
import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
abstract class BaseActivity<T : ViewDataBinding>(
@LayoutRes private val layoutRes: Int
) : AppCompatActivity() {
protected lateinit var binding: T
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,layoutRes)
binding.lifecycleOwner = this
}
}
액티비티
package com.spark.android.ui.base
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
abstract class BaseFragment<T : ViewDataBinding>(
@LayoutRes private val layoutRes: Int
) : Fragment() {
private var _binding: T? = null
protected val binding: T
get() = requireNotNull(_binding)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = DataBindingUtil.inflate(inflater, layoutRes, container, false)
binding.lifecycleOwner = viewLifecycleOwner
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
프래그먼트
'안드로이드' 카테고리의 다른 글
activity와 fragment에서 백버튼 처리하기 (0) | 2022.02.01 |
---|---|
바인딩 어댑터 매개변수 두개 이상일시 예시 (0) | 2022.01.26 |
깃관련 초보의 뻘짓 모음집 (0) | 2022.01.14 |
데이터바인딩에 리스너 붙여버리기 + 더러운코드를 어떻게 처리해야할까 (0) | 2022.01.14 |
리사이클러뷰 ItemDecoration 대체하기 (0) | 2022.01.11 |