Best practices for saving UI state on Android

Поділитися
Вставка
  • Опубліковано 27 гру 2024

КОМЕНТАРІ • 22

  • @AndroidDevelopers
    @AndroidDevelopers  Рік тому +9

    Watch all the Android and Play Sessions → goo.gle/IO23_androidplay_pin

  • @mdalikazi
    @mdalikazi Рік тому +1

    Amazing example of SavedStateProvider for View system!

  • @RowanGontier
    @RowanGontier Рік тому +1

    Love the passion, and command of subject. Just wish it was a lot more dumbed down, with clear as mud explanations of what we are trying to do.

  • @LiranBarsisa
    @LiranBarsisa Рік тому +1

    1:14 "it is impossible to completely opt out of activity recreation. Some configuration changes will always recreate the activity."
    Which ? When do they happen?
    As for the implementation here, it seems much much longer and complicated than what it used to be, of just using onSaveInstanceState, and the storage is also much longer and complicated compared to just SharedPreferences.
    What's the point in using those if the original method works fine and it's much simpler and shorter?
    This is true for both Compose and for the View model.

    • @scottbiggs8894
      @scottbiggs8894 10 місяців тому

      There are more ways to have configuration changes than simply orientation changes. Some android devices have mechanical keyboards that fold in and out. Some androids have screens that get bigger or smaller (Samsung Galaxy Fold, for example). Both of these are configuration changes that cannot be disabled by forcing the app to landscape or portrait mode.
      But you're very right--this does seem MUCH more complicated than the old method of using onSaveInstanceState and SharedPreferences. In my code, I am using a hybrid of SharedPreferences and ViewModel stuff. It's messy, but it also works in all cases. And it doesn't use any @Experimental annotations either.

    • @LiranBarsisa
      @LiranBarsisa 10 місяців тому

      @@scottbiggs8894 The examples you've mentioned are already covered if you check the available values in the configChanges.
      My question was: Which aren't supported?
      About ViewModel, it works only as long as the process is alive. Better to use onSaveInstanceState too.
      Of course, it depends on your needs.

  • @varadgalgali453
    @varadgalgali453 8 місяців тому

    At 12:45, the rememberNewsSearchState composable returns an object of type rememberSaveable.
    But, aren't composable functions supposed to have a Unit return type?

  • @mg508
    @mg508 8 місяців тому

    As for some previous comments about leaving the responsibility to the developer and/or SaveInstanceState being easier, it's quite the opposite. All the difference is in the viewModel being lifecycle-aware. With the saveInstanceState mechanism all the saving is done manually by the developer as the user navigates back and forth from one activity to the other. The view model, instead, not only survives configuration changes but also follows the activity's lifecycle hence saving and restoring the state accordingly.

  • @_runtime
    @_runtime Рік тому +3

    The first advanced case is quite interesting - a reusable UI element having a complex state (NewsSearchState) depending on a repository (NewsRepository). Since we don't use ViewModels there, it would be interesting to know what's the best practice of using that component. I assume that the repository will have suspend functions and in the UI element itself we cannot call those suspending functions with the coroutine scope available - it is a UI-bound scope but we need a view model scope to run the backend calls.

    • @deepakbisht4957
      @deepakbisht4957 Рік тому +3

      Same here.
      Looking for the best practices to do that. As we are developers we have a thing of keeping Ui and State less coupling in our mind with different layers of our architecture...

  • @llothar68
    @llothar68 Рік тому

    My best way: Safe them in the C++ business layer via NDK. I share all the logic for all the 5 big operating systems via C++. Of course if you app doesn't really do something ... it's different

  • @mdalikazi
    @mdalikazi Рік тому

    What is the mono font seen in the slides?

  • @walterjfranck
    @walterjfranck Рік тому +3

    me parece un montón de boilerplate entre Navigation y State Holder para integrar todo, agregar funcionalidad / personalización y mantener los estados en la navegación 🤯😭 ... Pero se entiende perfectamente

  • @kirwakelvinkering3122
    @kirwakelvinkering3122 11 місяців тому

    If viewmodels can survive configuration changes , what is the newd to intergrate it with SavedStateHandle api ?

  • @ismaelsanchez3492
    @ismaelsanchez3492 Рік тому +1

    Really confusing stuff, basically you are transferring the responsibility of what happens to the app when the system kills it to the developer, when this should be the responsibility of whoever kills the app in the first place. The system, Android, should be able to restore the app as it was, or at least, give the option to the developer of what to do, to restore it, restart it or personalize what to do and then yes, put it in the hand of the developer.

  • @KrisjanSenci
    @KrisjanSenci Рік тому

    Always save your states is a key

  • @codeone6423
    @codeone6423 10 місяців тому

    wow this was great thank you

  • @lost-prototype
    @lost-prototype Рік тому +2

    Very confusing

  • @JesusChrist-qf9mh
    @JesusChrist-qf9mh Рік тому +1

    This is not framework, it is just crazy, you are copying ios just copy it correctly without hoisting(this is what smart naming is all about) us and your selfs.

  • @unitbunsen7120
    @unitbunsen7120 Рік тому

    I won't to see look on the three