Here then.
So let's talk about the second way to save data in Android.
Using shared preferences shared preferences is an accidental fall in each which is stored in the application
context and in need to you can save data in the form of key value pairs.
This file is stored until the application is removed from the device or until the application and data
is cleared using share preferences.
You can store simple data types small pieces of data for example such as the number of points a user
has index game so that this amount is preserved when existing this application or for example a beggar
I'll call said by the user for this application to remain eat.
When exiting or splicing or to save the user logged in.
If he has already registered once and does not want to enter his date each time I name then you application
share preferences and indeed we will discuss how to use this data method that say the saving method.
Let me show how to create an object of this class the shared preference class written well so call their
variable shared preferences and then we need to assign the following value from the beginning.
We need a context we can write these.
In this case and then we can get different types of preferences.
As you can see the fears get shared preferences is displayed.
If we click on this method then in this case we get an Excel file at the level of the entire application
which can be accessed from the entire application.
If we choose another type get preferences then on the file that will be created.
The next email file will be available all at the level of this activity.
One activity main activity in this case which in which two is created this object.
Let's create an application level get shared preferences and then we need to pass the first parameter
string name and the second it mode the first name will be the name of the file.
We can call it whatever you like.
For example my shape preferences
and the next parameter that we must pass in this is Mort.
That is the mode in which these shared preferences will be available.
And this is specified in the following way context point and then we can choose mode we will select
more private so that this file is available only in our application.
And that's basically what our file is created.
And now we can put some data into this file with this name but in order to get access to edit in this
file we need to create another object of the shared preferences ed class
let's call it Ed and it is created as follows shared preferences.
EDIT And now using these editor we can save the data to all file shared preferences.
For example let's say some string to this file.
This is done as follows editor then put and you see the different types of data.
We can put boolean flawed.
We'll put string in this case put string and you need to specify where and when saving the intent or
intent.
Of care and value.
For example let's put a new value in it.
While hello world text which we have by default.
Let me in large to be seen here.
Let's give this a text to I.D.
and call the key for example new
text or new call or text
and proved the value for this key.
For example how low should preferences
and after all we put these pair of key and value peer into this file.
We need to save it.
Committed to using the following method Ed..
Come meet
but and Tutu tells us that consider using the blind method instead of cutting meat because the commitment
that writes data to the storage instantly while apply will do this.
In the background background modest profitable because it is not a blog you I user interface.
Therefore we can apply apply mantle let the same principle.
You can apply.
Both can meet and apply these messed up methods do the same thing just commit it does right away and
apply in the background.
And though the skin value bit is in our file and we can use it anywhere in the application.
Let's see now example we use it here in our own create method let's connect our text you with the layout
and set text for you in this text do you text from the file my shared preferences through the object
of their shared preferences class to do this from the beginning we need to extract value from this file
through the object we specify the object name shared preferences and then get string next bits of file
key new Hello takes
and the second parameter we need to specify the default value that will be set in the case if our file
in our file showed preferences.
There is no data on these key NewCo texts.
Let's specify for example default text
and let's now run the app and see what we've got.
And as you can see we have set a new text.
Let me increase the all later.
Stretch it like these.
Well now it seems so little better to see.
Hello shared preferences.
We have a text that is we have now created the file share preferences in the file system of our application
from which we extracted this data that is this data is already there.
Therefore if we remove this call now in which we put these key and value peer into all of it let's Coleman
tile this code so that it does not work and restart the application and let's see what happens.
And so as you see although in our application there is no court that places this takes an hour at file.
Nevertheless the text there already exists and the file is created this text this is extracted and installed
in our text you if we want to remove data from shared preferences we can do this in two ways.
Let me comment this code and comment only on the line and we should put all a pair of Cannavale so that
we have access to the editor now that we created in order to clear the data we can clear a specific
key value appear to do this we need to do the following editor remove and specify the key of the pier
whose value we want to delete the key is new Hello text so we can write NewCo text here
and then the value of this pair is deleted or we can do the following
specify the clear method and then all data from these shared preferences file is completely cleared
well it's for an application and see what happens
and you see we have the full text set since this key is not found we erase it therefore the default
text is set in the next we do we will analyse an example that is cause to life in which we will save
user data.