Android-CleanArchitecture-Kotlin icon indicating copy to clipboard operation
Android-CleanArchitecture-Kotlin copied to clipboard

Android Arch Components handle configuration changes / lifecycle

Open ashrafi opened this issue 6 years ago ā€¢ 4 comments

you said

At implementation level, in our example, MVVM is accomplished by the usage of Architecture Components, which its main advantage is to handle configuration changes when the screen rotates, something that has given us many headaches as android developers (I guess you know what Iā€™m talking about).

Disclaimer: that does not mean we have to no longer care about lifecycles, but it is way easier.

ViewModel was created to handle the configuration / lifecycle for you.

viewModel viewModel Lifecycle

ashrafi avatar Dec 10 '18 05:12 ashrafi

Don't forget about onSaveInstanceState though

Zhuinden avatar Dec 10 '18 09:12 Zhuinden

Good point but build in a way that I no longer need to use onSaveInstanceState :-)

Additionally, when you open an activity from an intent, the bundle of extras is delivered to you on both configuration changes and when the system restores an activity. If the search query were passed in as an intent extra, you could use the extras bundle instead of the onSaveInstanceState() bundle.

https://medium.com/androiddevelopers/viewmodels-persistence-onsaveinstancestate-restoring-ui-state-and-loaders-fc7cc4a6c090

ashrafi avatar Dec 10 '18 17:12 ashrafi

You can't throw every possible transient state and user input into an Intent.putExtra, because the user will start doing things after you open the Activity. šŸ¤”

And opening a new Activity for a search screen is just odd šŸ˜„ you typically have an edit text and it's "auto-complete"

Zhuinden avatar Dec 10 '18 17:12 Zhuinden

Also, the bundle has a size limit of about 1MB, which is not a problem if you are not going to put lists of objects. The main idea was to save ids and call again again the needed actions in order to retrieve your state.

Check this version of mvp that i had created. The presenter keeps the annotated data even if the app is killed by the OS. In addition to this, you do not need to stop a task an restart it during configuration changes. Maybe you can use a combination of ViewModel with my mvp in order to achieve what you expect.

You can check it here: https://github.com/St4B/mvp

St4B avatar Jan 30 '19 22:01 St4B