Viewmodel SavedStateHandle data lost after process death - android-savedstate

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?

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.

Related

How to solve error with View Binding in Kotlin

Here I'm getting this error
package lk.ac.kln.mit.stu.mobileapplicationdevelopment.activities
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View.inflate
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import lk.ac.kln.mit.stu.mobileapplicationdevelopment.R
class ShoppingActivity : AppCompatActivity() {
val binding by lazy{
ActivityShoppingBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_shopping)
setContentView(binding.root)
val navController = findNavController(R.id.shoppingHostFragment)
binding.bottomNavigation.setupWithNavContoller(navController)
}
}
Anyone know the reason to fail this code?
I tried adding below lines to the gradel as well
buildFeatures {
viewBinding true
}
You can better use this, this way the binding can be set in the onCreate as before this it can not build UI components.
var binding: ActivityShoppingBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityShoppingBinding.inflate(layoutInflater)
binding?.let {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_shopping)
setContentView(binding.root)
val navController = findNavController(R.id.shoppingHostFragment)
binding.bottomNavigation.setupWithNavContoller(navController)
}
}
ActivityShoppingBinding is in red because it's not recognised by the IDE - usually this means you haven't imported it, so it doesn't know what it is. And you don't have an import line for that class, so that's probably your problem!
The easiest fix is just to put your cursor over the error and do Alt+Enter (or whatever) or click the lightbulb icon that appears, and the IDE will offer to import it for you. Once that binding class is imported it should work fine.
Also you're calling setContentView twice - that's pointless (you're inflating a layout then throwing it away immediately) and it can introduce bugs if you accidentally set things on a layout that isn't being displayed. You should only be calling it once, with binding.root in this case.
You might want to look at the recommended way of initialising view binding in Activities (and the Fragments section too if you're using them). Avoid using lazy for UI stuff, since it can only be assigned once you can run into issues (mostly with Fragments, but the example in that link uses lateinit too)

Unresolved reference in findViewById when using Glide in Fragment in Kotlin

I am new to Kotlin. I have an "unresolved reference:findViewByid" when I am using Fragment. I try to add "view?.findViewByid", but the app crashed and val imgUniverse returns "null". Is there a way to solve it?. Thanks.
I am trying to implement swipe pictures on different xml on multiple screens using PageViewer2.
KC
class Page1Fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val imgUniverse = findViewById<ImageView>(R.id.image_universe) // Unresolved reference in findViewById
val imgFooter = findViewById<ImageView>(R.id.image_footer) // Unresolved reference in findViewById
Log.d("testing2: " , imgUniverse.toString())
Glide.with(this)
.load(R.drawable.universe3)
.fitCenter()
.centerCrop()
.into(imgUniverse!!)
Glide.with(this)
.load(R.drawable.footer)
.fitCenter()
.centerCrop()
.into(imgFooter!!)
return inflater.inflate(R.layout.fragment_page1, container, false)
}
}
You are searching for views before you are inflating (creating) them. The view is created in the last line with by inflating it using inflater.inflate(). Before that, none of the views even exists and therefore cannot be found and used.
Move all the code (except for the last line) into onViewCreated() and it will work.

Viewmodel seems to be scoped to a fragment instead of activity. I am using navigation component as well

I am using navigation component and I have one activity in the app with so many fragments. I am trying to use one view model scoped to the activity in all the fragments but it working only in the 3 fragments nested in my home fragment. The home fragment which is the navigation host fragment is using a view pager to add 3 tabs which are 3 fragments within the home fragment. For instance I have like 3 tabs A, B, C. Between these three I can share data in view model successfully. 'A' has a detail fragment lets call it D. when I try to access shared viewModel in D it has null values which I know I have set already in A. I have a normal view model class.
in each fragment I instantiate view model like this
private val viewModel: MainActivityViewModel by activityViewModels()
it seems the viewModel is only scoped to the home fragment because it not working inside any detail fragment that I navigate to. Thank you. !
just in case there is something unusual about my view model class here is how it looks like
class MainActivityViewModel: ViewModel() {
var itemListLiveData = MutableLiveData<List<Item>>()
fun setItems(itemList: List<Item>) {
itemListLiveData.value = itemList
}
fun getItems() = itemListLiveData.value
}
You have to initialise your viewModel in onCreate method of your fragment.
// declare your viewModel like this
lateinit var viewModel : MainActivityViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// other code
// initialise your view model here
viewModel = ViewModelProvider(requireActivity()).get(MainActivityViewModel::class.java)
// other code
}
Also there is one mistake in your MainActivityViewModel class:
fun setItems(itemList: List<Item>) {
// set your live data like this
itemListLiveData.postValue(itemList)
}
Try instantiating your viewmodel like this
lateinit var viewModel: MainActivityViewModel
and in onViewCreated
viewModel = (activity as MainActivity).viewModel
This worked for me in the past when sharing a ViewModel.
Oops ! sorry I found the problem which has nothing to do with the activity view model not working. but I just leave my answer in case by chance some one else has the same issue.
The problem was here
fun setItems(itemList: List<Item>) {
itemListLiveData.value = itemList
}
in the fragment where I set this itemList, I clear this itemList in the fragments onStop() method so because itemListLiveData.value is referencing this itemList object it is also cleared. So I resolved it by instantiating it with a new object of itemList instead of referencing the itemList object. like below
fun setItems(itemList: List<Item>) {
itemListLiveData.value = ArrayList<Item>(itemList)
}

navController.currentDestination + findFragmentById returns null

I'm trying to find the id for the last fragment navigated to using navController since I'm using the Navigation component instead of fragment managers:
override fun onBackPressed() {
val fragment = this.supportFragmentManager.findFragmentById(navController.currentDestination!!.id)
(fragment as IOnBackPressed).onBackPressed().not().let {
if (it) super.onBackPressed()
}
}
However, I keep getting the following error message when I trigger this code:
kotlin.TypeCastException: null cannot be cast to non-null type com.google.apps.sanctified.IOnBackPressed
at com.google.apps.sanctified.SanctifiedActivity.onBackPressed(SanctifiedActivity.kt:41)
I walked through the code with the debugger, and it appears that navController is finding the correct destination in its backstack and returning the correct id. However, findFragmentById() doesn't seem to have any record of the fragment, and always returns null. The documentation says that this function:
Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction.
Obviously I'm not using transactions, but it should be updated at inflation. Perhaps I'm doing something wrong there:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = DetailFragmentBinding.inflate(inflater, container, false)
return binding.root
}
Needed to use this:
navHost.childFragmentManager.findFragmentById(R.id.nav_host)

How to get a RecyclerView Item to update itself from a LiveData value in it's binding object using BindingAdapter?

I have a recyclerview.widget.ListAdapter to display some cards in a grid. When I click on a card, it comes to the front of the grid, flips, and when you see the front, you are supposed to be able to like it. I have an ImageView on the corner of the front of the card, that is supposed to toggle between liking or not, displaying a different drawable that I have already defined.
My ViewHolder looks like this:
class GameCardViewHolder(private val binding : ItemGameCardBinding) :
RecyclerView.ViewHolder(binding.root){
companion object {
fun from(parent: ViewGroup): GameCardViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemGameCardBinding.inflate(layoutInflater, parent, false)
return GameCardViewHolder(binding)
}
...
}
fun bind(productCard: ProductCard, listener: OnClickListener){
binding.productCard = productCard
binding.executePendingBindings()
...
}
}
My item_game_card.xml looks like this:
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="productCard"
type="company.app.model.ProductCard" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout>
...
<ImageView
android:id="#+id/gameItemLikeImageView"
android:layout_width="0dp"
android:layout_height="0dp"
...
app:imageDrawable="#{productCard.liked}"
android:onClick="#{() -> productCard.toggleLike()}"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
My BindingAdapters.kt:
#BindingAdapter("imageDrawable")
fun bindImageDrawable(imgView: ImageView, boolean: LiveData<Boolean>?) {
Timber.d("Binding liked: $boolean")
when(boolean?.value){
true -> imgView.setImageResource(R.drawable.ic_card_like)
false -> imgView.setImageResource(R.drawable.ic_card_dislike)
}
}
And my ProductCard.kt:
data class ProductCard(
val position: Int,
val product: Product
){
...
private val _liked = MutableLiveData(false)
val liked : LiveData<Boolean>
get() = _liked
fun toggleLikeProduct(){
Timber.d("Toggle like before: ${_liked.value}")
val oldValue = _liked.value!!
_liked.value = !oldValue
Timber.d("Toggle like after: ${_liked.value}")
}
}
When I click on the ImageView, the console prints out the before and after values every time correctly, so I know the value of the liked LiveData is actually changing, but the View is not updating accordingly, that is because the binding adapter is only being called once (in the binding.executePendingBindings() I guess, when the RecyclerView binds the ViewHolder, and not every single time the LiveData changes).
I want the BindingAdapter code to be run every time the liked LiveData changes.
When I do this with ViewModel and Fragment layout, every time a LiveData changes in the ViewModel, the View changes because the xml is referencing that LiveData, but I don't know why, when it comes to ProductCard and recyclerview item layout, even if the xml is referencing that LiveData, the View doesn't change, the xml is not actually binding to the LiveData, which is the whole point of having LiveData.
I have already acheived this behaviour with a very brute, unelegant sort of way, setting an onClickListener on that view, and forcing it to change it's resource drawable, on the OnBindViewHolder like this:
fun bind(productCard: ProductCard, listener: OnClickListener){
binding.productCard = productCard
binding.executePendingBindings()
binding.gameItemLikeImageView.setOnClickListener {
it?.let {
val card = binding.productCard
Timber.d("Tried changing liked status: ${card!!.liked}")
val old = card.liked
card.liked = !old
binding.productCard = card
val card2 = binding.productCard
Timber.d("Tried changing liked status: ${card2!!.liked}")
if (!old){
it.setBackgroundResource(R.drawable.ic_card_like)
}
else {
it.setBackgroundResource(R.drawable.ic_card_dislike)
}
}
}
...
}
But I would really like that the behaviour of each card be controlled by the state of the ProductCard object that it is bind to.
I have also tried to force call binding.executePendingBindings() every time I click, but also didn't work.
How can I make the item_game_card.xml to observe the changes in the LiveData?
app:imageDrawable won't accept any LiveData<Boolean> and I'd wonder why it isn't LiveData<Product>? Try to bind the initial value alike this:
app:imageDrawable="#{product.liked ? R.drawable_liked : R.drawable_neutral}"
For LiveData the LifecycleOwner needs to be set for the binding:
class ProductsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Inflate view and obtain an instance of the binding class.
val binding: ProductsBinding = DataBindingUtil.setContentView(this, R.layout.products)
// Specify the current activity as the lifecycle owner.
binding.setLifecycleOwner(this)
}
}
see the documentation.