title: SavedStateHandle returning null values
body: <p>I have an <code>ActionBar</code> menu icon that opens a <code>CategoryFragment</code>. This fragment takes in a <code>category</code> object <code>SafeArgs</code> argument passed from another fragment. In the <code>CategoryFragment</code>, I store the <code>category</code>'s name and id into the fragment's shared <code>ViewModel</code> as <code>SavedStateHandle</code> values. I've setup it up so that the fragment uses the stored <code>SavedStateHandle</code> values for the category name and id when it needs to. For example, for the first time, the <code>CategoryFragment</code> uses the category object passed from the sending fragment, but subsequent creation of the <code>CategoryFrgament</code> will use the <code>SavedStateHandle</code> values.</p>
  
  <p>The problem is, if after first opening <code>CategoriesFragment</code> and then exiting the app by either pressing the phone's physical back button or terminating the app from the phone's recent's button in the navbar, now opening the <code>CategoryFragment</code> directly by pressing the ActionBar menu icon displays a blank screen. This is because the values returned from <code>SavedStateHandle</code> are null. How can I fix this?</p>
  
  <p><strong>Category Fragment</strong></p>
  
  <pre><code>class CategoryFragment : Fragment(), SearchView.OnQueryTextListener {
  
      lateinit var navController: NavController
      private var adapter: TasksRecyclerAdapter? = null
      private val viewModel: CategoryTasksViewModel by activityViewModels()
      private var fromCategoriesFragment: Boolean = false
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_category, container, false)
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
          navController = Navigation.findNavController(view)
  
          observerSetup()
          recyclerSetup()
  
          var searchView = category_tasks_searchview
          searchView.setOnQueryTextListener(this)
  
          fab_new_task.setOnClickListener {
              navController.navigate(R.id.action_categoryFragment_to_newTaskDialogFragment)
          }
  
          showTasks()
      }
  
  private fun showTasks() {
  
          if(fromCategoriesFragment){
  
              PomoPlayObservablesSingleton.fromCategoriesFragment.onNext(false)
  
              if (!arguments?.isEmpty!!) {
  
                  var args = CategoryFragmentArgs.fromBundle(arguments!!)
                  category_title.text = args.category?.name
  
                  var category = args.category
                  viewModel.setPomoCategoryName(category.name)
                  viewModel.setCategoryId(category.id)
                  viewModel.searchTasksByCategoryId(category.id)
  
              }
  
          }
          else{
              category_title.text = viewModel.getPomoCategoryName()
              viewModel.searchTasksByCategoryId(viewModel.getCategoryId())
  
              Log.i("CategoryFrag-CatName", viewModel.getPomoCategoryName().toString())
              Log.i("CategoryFrag-CatId", viewModel.getCategoryId().toString())
          }
      }
  
      private fun observerSetup() {
  
          viewModel.getSearchTasksByCategoryIdResults().observe(this,androidx.lifecycle.Observer { tasks -&gt;
  
              if(tasks.isNotEmpty()){
  
                  adapter?.setTasksList(tasks.sortedBy { task -&gt; task.name?.toLowerCase() })
                  task_not_found_bubble.visibility = View.GONE
                  task_not_found_text.visibility = View.GONE
              }
              else{
                  task_not_found_bubble.visibility = View.VISIBLE
                  task_not_found_text.visibility = View.VISIBLE
              }
          })
  
          PomoPlayObservablesSingleton.fromCategoriesFragment.subscribe {value -&gt; fromCategoriesFragment = value}
  
      }
  
      private fun recyclerSetup() {
          adapter = context?.let { TasksRecyclerAdapter(it) }
          tasks_list?.layoutManager = LinearLayoutManager(context)
          tasks_list?.adapter = adapter
      }
  
      override fun onQueryTextSubmit(query: String?): Boolean {
  
          Log.i("Lifecycle-CatFragment", "onQueryTextSubmit() called")
  
          var q = query?.toLowerCase()?.trim()?.replace("\\s+".toRegex(), " ")
          setLastSearchQuery(q.toString())
  
          viewModel.searchTasksByName(viewModel.getLastSearchQuery().toString())
  
          return false
      }
  
      private fun setLastSearchQuery(lastSearchQuery: String) {
          viewModel.setLastSearchQuery(lastSearchQuery)
      }
  }
  </code></pre>
  
  <p><strong>CategoryTasksViewModel</strong></p>
  
  <pre><code>class CategoryTasksViewModel(application: Application, state: SavedStateHandle) : AndroidViewModel(application) {
  
      private val repository: PomoPlayRepository = PomoPlayRepository(application)
      private val allCategories: LiveData&lt;List&lt;Category&gt;&gt;?
      private val allPomoTasks: LiveData&lt;List&lt;PomoTask&gt;&gt;?
      private val searchCategoriesByNameResults: MutableLiveData&lt;List&lt;Category&gt;&gt;
      private val searchCategoryByIdResults: MutableLiveData&lt;Category&gt;
      private val searchTasksByIdResults: MutableLiveData&lt;PomoTask&gt;
      private val searchTasksByNameResults: MutableLiveData&lt;List&lt;PomoTask&gt;&gt;
      private val searchTasksByCategoryIdResults: MutableLiveData&lt;List&lt;PomoTask&gt;&gt;
  
      private val savedStateHandle = state
  
      companion object{
  
          private const val LAST_SEARCH_QUERY = "lastSearchQuery"
  
      }
  
      init {
          allCategories = repository.allCategories
          allPomoTasks = repository.allPomoTasks
          searchTasksByIdResults = repository.searchTasksByIdResults
          searchTasksByNameResults = repository.searchTasksByNameResults
          searchTasksByCategoryIdResults = repository.searchTasksByCategoryIdResults
          searchCategoryByIdResults = repository.searchCategoriesByIdResults
          searchCategoriesByNameResults = repository.searchCategoriesByNameResults
      }
  
      fun setLastSearchQuery(lastSearchName: String){
          savedStateHandle.set(LAST_SEARCH_QUERY, lastSearchName)
      }
  
      fun getLastSearchQuery(): String?{
          return savedStateHandle.get&lt;String&gt;(LAST_SEARCH_QUERY)
      }
  
      fun setPomoCategoryName(name: String?){
          savedStateHandle.set("categoryName", name)
      }
  
      fun getPomoCategoryName(): String?{
          return savedStateHandle.get&lt;String&gt;("categoryName")
      }
  
      fun setCategoryId(id: Int){
          savedStateHandle.set("categoryId", id)
      }
  
      fun getCategoryId(): Int?{
          return savedStateHandle.get&lt;Int&gt;("categoryId")
      }
  
      fun insertTask(pomoTask: PomoTask?) {
          repository.insertTask(pomoTask)
      }
  
      fun deleteTask(pomoTask: PomoTask) {
          repository.deleteTask(pomoTask)
      }
  
      fun updateTask(pomoTask: PomoTask) {
          repository.updateTask(pomoTask)
      }
  
  
      fun searchTasksByName(name: String) {
          repository.searchTasksByName(name)
      }
  
  
      fun searchTasksById(pomoTaskId: Int){
          repository.searchTasksById(pomoTaskId)
      }
  
      fun searchTasksByCategoryId(categoryId: Int?){
          repository.searchTasksByCategoryId(categoryId)
      }
  
      fun getAllPomoTasks() : LiveData&lt;List&lt;PomoTask&gt;&gt;? {
          return allPomoTasks
      }
  
      fun getSearchTasksbyNameResults() : MutableLiveData&lt;List&lt;PomoTask&gt;&gt; {
          return searchTasksByNameResults
      }
  
      fun getSearchTasksByIdResults() : MutableLiveData&lt;PomoTask&gt; {
          return searchTasksByIdResults
      }
  
      fun getSearchTasksByCategoryIdResults() : MutableLiveData&lt;List&lt;PomoTask&gt;&gt; {
          return searchTasksByCategoryIdResults
      }
  
  }
  </code></pre>