Saving User Information to Local Storage Using Shared Preferences

Ufinix Academy
A free video tutorial from Ufinix Academy
Learn programming and app development
4.3 instructor rating • 4 courses • 4,828 students

Learn more from the full course

Xamarin Android Uber Clone App with C# and Firebase

Learn advanced Xamarin Android concepts by building a fully complete and functional Uber Clone app using Firebase

15:26:55 of on-demand video • Updated July 2020

  • Learn to build complete Uber Clone apps (driver and passenger) with C#, Xamarin and Firebase
  • Learn to create, remove, update and delete records in Firebase Database
  • Save and handle data locally using Shared Preferences
  • Learn how to use Google Location and Map services in your Apps
  • Learn to find direction from one point to the other with Google Direction Api
  • Learn to find place address with Location Coordinates using Google Geocoding API
  • Learn advanced usage of C# Lambda Expressions
  • Learn advanced usage and creation of custom C# Event handlers
  • Learn to create and design beautiful User Interfaces
  • Understand how to create Local Notifications using Android Notification Manager
  • Learn to make asynchronous WebCallls using Rest Api
  • Learn to initiate a phone call from your app using Native Intents
English [Auto] OK so guys do they show it's lecture. I want to show us how to save our user information to a local storage and want to use our shared preferences for these particular purposes. And the reason is because at some point we may need to have assets reduce the user information when we don't have network. All right. OK so I'm going to just show us how to save our user information to our shared preferences. Of course later on in the app we'll be making very good use of it. All right. So to do that I'm going just to create a function like I said I like to keep things pretty organized and clean. So I'm going to create a new mentor and call it save save to share and preference shared preference. OK so what I'm gonna do is I'm going to define. So I'm going to define an instance of shared preferences. OK. So to do that I say I shared preferences prev preferences preferences equal to application that contests don't get shared preferences I call these user info file racial mode should be private so the nice thing is we're going to create a new age shared preference in detail. All right. So I see a show frame reference references a detail. So it is the instance we're going to be using to be a right team data to our shared preferences. OK. So this instance is what we are going to be using to write data. OK to our local storyteller I'm going to call that a detail. OK. So now our editor we'll be equal to references that Edit Ok so we are ready to start writing information to our editor. So in the end we send that information to our local storage. All right. So how to do that is we going to see a detail put streaming so you can see either you know can save on our variable times our asides from stream. All right as you can see you can see bullying flowed into ga long on the right. So by now we are anticipating a stream. OK. You know data saved in shop references are saved in a key value for months. OK so we're going to provide a key for our e-mail quality e-mail quality e-mail as the key and this key is very important because that's what we'll be using to retrieve the data whenever we want it. So what we want to save here the value for the email will be our e-mail address that the user provided a detail that string OK. So call these full name and assign the value of full name to it. So the last one is a detail that puts string in a our phone number of course to this right. So we're going to put our for our funding so any data with string the key should be for name because the value will be the value of our full name The full name that the user provided when he or she was saying registering to the app. With string the last one will be the phone number King phone. All right. So after you're done writing your data to the preference in detail you want to have to add this command edit or apply this receive your information. This information you provided to your local storage. All right. So dies to retrieve these data is very simple don't we may not be able to take this out now in case we want to know how to do this right away but eventually you know later on on the app I'll be showing us how to do that but to retrieve these data is very simple. So let me just show us how to do that. So I would just create a new method and quickly retrieve data routine data. OK so he had to retrieve my email address string Email I'm gonna have to say I'm gonna have to define these shared preferences as a local as a global variable or so that I can be accessible from anywhere in the within my registration activity. Okay. So this is just to show us how to retrieve data from a shared preferences door. We'll get deeper with that later you know and there's no one to do is to start rooted in the values and to be able to do that we need to know the keys of their value they'll want to retrieve as you can see the key here is e-mail so we can use this key to retrieve the value of our e-mail. And to do that I can't go ahead and say string Email ackwards to preferences. Dot gets train ride because we know that while we save the easy string. So now we have to provide the key the String key which is e-mail and I venture that there is no value saved there what should be the default value. I want you to be an empty string right. So guys this is how simple it is serious to data that are saved to these shared preferences. These just how simple easy you better for it. And this is how simple it is to write the anatomy. So guys this is just a pig. You know this is just a quick class in between. So now we just have this knowledge because we'll be using it later on in the class. So guys see you in an s class.