To save data in Android Studio using Kotlin we need to utilize SharedPreferences. In this tutorial we'll wire up the necessary Kotlin and XML code to actually save information directly to the Android device.
In our MainActivity we'll setup our getSharedPreferences method and call getString, setting a default value if the key doesn't exist. And it won't; we haven't saved anything yet.
Which is why on our Add Product Activity screen we'll setup an onClickListener to to process the product name every time the user hits the submit button. Kotlin provides a nice setOnClickListener lambda that we'll use to trigger the SharedPreferences code.
Just remember to close then reopen the app when doing this tutorial. We'll fix that in the next video. Also, to clear the storage (the nuclear option), simply go to Settings on Android, locate the app and hit Clear Storage.
Android is a fun platform, and saving stuff directly to disk is certainly something I couldn't do back when I was a web developer! But Android apps are powerful: GPS, camera, storage, faster processing, etc.
As always, the diff for this lesson is here: https://github.com/danielmalone/Andro... (You can also browse the code for the entire app at this link.)
Connect with me on LinkedIn! / danielmalone
Смотрите видео Android Studio Tutorial - Part 4 (2020 Edition) онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь DJ Malone 07 Март 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 43,038 раз и оно понравилось 523 людям.