Android Studio Tutorial - Part 4 (2020 Edition)
DJ Malone DJ Malone
41.7K subscribers
42,941 views
0

 Published On Mar 7, 2020

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  

show more

Share/Embed