Fix onSaveInstanceState Not Working (FoSNW)




AVAILABLE:    5

Hi guys, this time I want to share my experience in overcoming the official Android bug caused by these Indian guys.

It seems like they are starting to ignore the negative impacts caused by their innovations, whereas in the era before being dominated by Indians, Android looked elegant and smooth from time to time in every development.. but this time, there are so many damn bugs that I found and I have to use shamanic knowledge to overcome them, one of which is related to:

override  fun  onSaveInstanceState (outState: Bundle)

override  fun  onRestoreInstanceState (savedInstanceState: Bundle)

How could it not be, I found this bug on May 20, 2024 after our client complained about the unusual UX.

Finally I concluded myself that both overrides have been removed from the Android lifecycle, so what can I do, I have to use a manual method to save the UI state that I want.

Step 1 - Initialize Variable Bundle via onCreated

class  Abc : Activity{
     private  var savedSate: Bundle? = null 
    override  fun  onCreate (savedInstanceState: Bundle?) {
         //initialize here
        savedSate = savedInstanceState
    }
}

Step 2 - Save Your UI State

Store your UI state wherever you want, in this case, I store my nested recyclerview ui state to maintain the last scroll position ui state of the parent and child recyclerview I have.

    private  fun  saveUIState (){
        rvState = listManager?.onSaveInstanceState()
        rvStateChild = childManager?.onSaveInstanceState()
        savedSate?.putParcelable(RV_STATE, rvState)
        savedSate?.putParcelable(RV_STATE_CHILD, rvStateChild)
    }

I call it on every recyclerview item click event.

Step 3 - Restore Your UI State

You should be familiar with this observer, of course I use the MVVM pattern, so I restore my sate UI every time the fetching of data from the server is successful.

private  val prepareHistoryDetail = Observer<ResponseWeeklyReportMkt> {
    rvState?.let { listManager?.onRestoreInstanceState(rvState) }
}

Post a Comment

Previous Next

نموذج الاتصال