[코틀린] Save state

Save state

1. onSaveInstanceState() & onRestoreInstanceState()

class MainActivity : AppCompatActivity(){
...
	override fun onSaveInstanceState(outState : Bundle){
		super.onSaveInstanceState(outState)
		outState.putInt("count", viewModel.count)
	}
	
	override fun onRestoreInstanceState(savedInstanceState: Bundle){
		super.onRestoreInstanceState(savedInstanceState)
		viewModel.count = savedInstanceState.getInt("count")
	}
}

//onSaveInstanceState()에서 인수로 전달되는 outState 번들에 키와 함께 value를 저장.
//onRestoreInstanceState()를 통해 저장된 것으로 데이터를 복구.

2. SaveStateHandle

class MainViewModel(private val handle : SavedStateHandle) : ViewModel(){
	private var count = handle.get<Int>("count") ? : 0
		set(value){
			field = value
			//value 값 확정시킴.
			countLiveData.value = value
			handle.set("count", value)
			//setter를 override하면서 동시에 수행될 코드들 삽입.
		}
    val countLiveData : MutableLiveData<Int> = MutableLiveData<Int>()
    ...
}

//ViewModel의 경우 생성자 인자로 (private val handle : SavedStateHandle)만 널어주기만 하면 된다.