Viewmodel SavedStateHandle data lost after process death

1.1k views Asked by At

I am trying to use savedStateHandle in my ViewModel and test it. My simple case is that I'm just storing an Int value in the SavedStateHandle and trying to get it again after the process death. But its not working at all. I am using the following dependency

implementation 'androidx.activity:activity-ktx:1.2.0-alpha08'
implementation "androidx.fragment:fragment-ktx:1.3.0-alpha08"

Below is my Fragment which is having one button and one TextView. When I click the button, number 5 is stored in the savedStateHandle of the ViewModel and then the same was observed via the getLiveData method of the savedStateHandle and the number is displayed in the TextView. So, after process death, it should correctly restore the value of 5 and display it in the text view. Following is my fragment code

class FirstFragment : Fragment() {

    private val viewModel by lazy{
        ViewModelProvider(this).get(FirstFragmentVM::class.java)
    }


    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

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

        button_first.setOnClickListener {
            viewModel.rememberNumber(5)
        }

        observeViewModel()
    }

    private fun observeViewModel(){

        viewModel.rememberedNumber.observe(viewLifecycleOwner){
            textview_first.text = it.toString()
        }

    }

}

and the following is my ViewModel code

@ExperimentalCoroutinesApi
@FlowPreview
class FirstFragmentVM(savedStateHandle: SavedStateHandle) : ViewModel() {

    companion object{
        private const val KEY_NUMBER_TO_REMEMBER="number:to:remember"
    }

    private val savedState=savedStateHandle
    val rememberedNumber=savedState.getLiveData<Int>(KEY_NUMBER_TO_REMEMBER)


    fun rememberNumber(number:Int){
        savedState.set(KEY_NUMBER_TO_REMEMBER,number)
    }

}

When run this app and click the button, the number 5 is stored in the savedStateHandle and its displaying "5" correcly in the text view. But when I put the app in the background and kill the process using adb and then restart the process from the recent screen, the entire app is restarted and its not showing the remembered number in the textView. Instead, its showing "Hello" which I set in the layout xml. I am killing the process as follows

adb shell
am kill <package name>

Anyone kindly help me why its not at all working for me? What am I doing wrong here?

1

There are 1 answers

1
Sundaravel On

Finally, I found the reason. The app restarts if we kill the app process after launching it directly from the IDE. Instead, If I close the app by swiping it from recents, launch it from the app drawer and then killing it via adb works and restores the state of the app perfectly.