본문 바로가기

안드로이드 스튜디오

[안드로이드 스튜디오 코틀린] MVVM 패턴 BaseActivity, BaseFragment 적용(ViewBinding)

728x90
반응형

 

 

 

 

 

ViewBinding을 사용한 MVVM 패턴 Base Code

 

 

 

 

 

 

 

 

 

BaseActivity.kt

 

import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.viewbinding.ViewBinding

abstract class BaseActivity<VB: ViewBinding, VM: ViewModel>: AppCompatActivity() {

    protected lateinit var binding : VB
    protected lateinit var viewModel: VM

    abstract fun getViewBinding(): VB
    abstract fun createViewModel(): VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = getViewBinding()
        viewModel = createViewModel()
        setContentView(binding.root)
    }
}

 

 

 

 

 

 

 

 

* viewmodel도 getViewModel() 함수로 만들면

Platform declaration clash: The following declarations have the same JVM signature (getViewModel()Landroidx/lifecycle/ViewModel;): fun `<get-viewModel>`(): VM defined in 에러가 발생한다

 

 

 

그 이유는 AndroidX 라이브러리의 ViewModel 클래스의 getViewModel() 함수 시그니처가 충돌하여 발생하기 때문이다

-> 때문에 createViewModel()함수로 만들어준다.

 

 

 

 

 

 

 

 

 

 

 

 

 

BaseFragment.kt

 

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.viewbinding.ViewBinding

abstract class BaseFragment<VB: ViewBinding, VM: ViewModel>: Fragment() {

    protected lateinit var binding: VB
    protected lateinit var viewModel: VM

    abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB
    abstract fun createViewModel(): VM

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = getViewBinding(inflater, container)
        viewModel = createViewModel()
        return binding.root
    }
}

 

 

 

BaseViewModel.kt

 

 

 

import androidx.lifecycle.ViewModel

abstract class BaseViewModel: ViewModel() {


}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MainActivity.kt

 

 

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.example.mvvm.base.BaseActivity
import com.example.mvvm.databinding.ActivityMainBinding
import com.example.mvvm.viewmodel.activity.MainViewModel

class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {

    override fun getViewBinding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }


    override fun createViewModel(): MainViewModel {
        return ViewModelProvider(this)[MainViewModel::class.java]
    }
    

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

}

 

 

 

 

 

HomeFragment.kt

 

 

 

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.example.mvvm.base.BaseFragment
import com.example.mvvm.databinding.FragmentHomeBinding
import com.example.mvvm.viewmodel.fragment.HomeViewModel

class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>() {

    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(layoutInflater)
    }
    

    override fun createViewModel(): HomeViewModel {
        return ViewModelProvider(this)[HomeViewModel::class.java]
    }
    

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }
}
728x90
반응형