Hey there.
So what is a preference change listen.
Well we already implemented in our main activity and in settings fragment on shared preference change
listener interface.
This interface serves to monitor changes in the shared preferences flight while the other preference
change listener interface monitors the changes of one particular setting.
And it is written as follows.
As you remember several interfaces can be implemented and we implement in our fragment and the second
on change on preference change listener interface and then implement the on preference change method.
It appears here and as you can see this is not the ward but bullying method it returns the value boolean
by default.
It returns false.
So once again shared by inference changed listener is watching the file shaped preferences when a certain
value is placed in this file then it's on shared preference changed method is turned on.
This is method.
This method on preference change method is turned on when some specific setting is changed and it is
turned on before the value is placed in the shared preference file.
And even these on preference change method returns false.
Then the value that the user is trying to enter into the setting will not be placed in the shared preference
spot.
If the method returns true then this value will be placed in the shared preferences spot.
And after that on shared preference changed listen that is call.
And as I said this interface is set for a specific setting in our own create preferences method.
Let's create an object of the preference CLOs
and assign a value using fine preference and enter here key of our index preference.
The folding table of these key and then for these specific setting for added tax preference we set preference.
Change listener or right preference set preference.
Set preference change listener.
And write these here and so on preference change listener is set for this setting.
And now we can implement implement its behavior.
Let's here first check if this setting preference get key.
There is such a method to get the key and if this key is equal to our key.
This is the default interval.
And here we create them first.
So the three default interval
default interval 3 includes name it and assign these object which we get as the parameter 0.
But since it is a different object of different classes we need to cost it to the string class and then
we write the block try catch and write in the default interval assign integer parts int we recognize
parse it as an integer and specify the string default interval string that we try to recognize the string
as an integer and do it in the tribe block.
If we succeed then the court will continue to execute and we will return true.
Otherwise then we need to specify the gauge book.
No format exception in the air and let's name it and in the blog catch we need to tell the user that
he's entering an incorrect value let's create toast for these
thoughts.
Make text get context then specify the text that we will bring to the user.
Please enter
an integer
number and not specify the length whether the bill on and here we will run in the cash BLOCK That's
right.
Toast show me.
Uh there is an exception.
Let's remove the try catch block in our activity in main activity
since
on shared preference.
Change will not be called if the incorrect value centered.
As I told you already.
In this case we will return false.
Here and the value will not be placed in shared preferences for the onset.
Preference changed method will not be called let's run the app.
Ha it crashed again because so far this shit preferences has the wrong value so let's return to our
catch block here.
Around the application then change the value to the correct and only then remove the code here
let's change the value let it be 30 seconds.
Okay
and now we can off to the shared preferences in it.
We have correct value we can remove this try catch and relaunch the application
and now
if we tried to put an incorrect value then please enter an Integer 0 number is displayed and as is see
the value in their shared preferences file does not change 30 remains.
And if we enter a valid value then it changes and here everything set correctly.
Well let's try to start the timer to understand that everything works for says before
everything is great.
And so we learned a lot with you in the cool time application and now it's time to introduce the storage
of data in a database.