Sometimes you need your application to do something in the background and therefore we are going to
use this do in background method, so why do we need that?
Well, for example, if we want to store an image, as we will in the next video, we want to store
an image of the user that he has created in his application.
Right.
And that he has drawn, then we need to do that in the background because the storage process can take
a while.
It can take maybe half a second, maybe it's just a couple of milliseconds.
But still, it will take a little while.
And during that time, the UI thread will be blocked.
So if you don't do that in the background, it will be blocked.
So that's why it makes sense to do things that will take a while in the background.
For example, loading a file from the Internet, do that in the background, not in the foreground,
blocking your UI thread, which will then lead to your UI not responding because if you're rewa, it's
not responding.
That's a really great indicator for your application to suck and for your users to uninstall it, because
when they use it and it's blocking and it's freezing, they don't want to use it anymore.
So how do we accomplish to run something in the background?
Because what I have here is just a for loop, which will loop from one to one thousand.
Maybe we should do one to ten thousand.
So it takes a little longer.
But what I have is that this doing background function should just return value so you can give a little
message to unposed execute.
So what is on post to execute well in order to get there?
Let's start from the beginning.
So we have this little application or created this little application which will just have a button
here saying once you click on it, execute an async task.
So execute an asynchronous task.
So a task which is running asynchronously, which means it will run in the background.
So the value that I pass here is just background tasks executed successfully.
So that is what should be passed over to this execute async task.
And then I execute that task.
So I called this method called execute, and that's how you actually start such an async task.
So what is this async task, you might wonder?
Well, this execute async task is actually an async task that I created myself.
So this is an inner class, which I'm in private.
So it's only available within my main activity.
It's not going to be available anywhere else.
And it takes in a value of type string and inherits from this class called async task, which is generally
the class that you use when you want to run asynchronous tasks.
So if you want to run something in the background, such as a bitmap async task, which we are going
to need because we will need to create a bitmap out of the image that the user has drawn or when you
want to download something from the web.
So, for example, just an JSON file, which has a bunch of data that you want to display in your application,
then you would also do that in the background, as well as images that you would download from the Web
and so forth.
So that's what you would do in the background.
You don't want to block your main threat or your UI for it while you download it.
OK, so what we need here is a dialogue, or at least what I have created here is just a dialogue because
I want to display a dialogue once we start executing.
So just before we start executing, to be more precise.
So there is this overwrite fun on pre execute.
This is the function that will be called just before the execution starts.
So you have an order of on pre execute, doing background and on to execute.
So that is the order, number one, pre execute.
So whatever we need to prepare then do in background run the actual stuff that should run asynchronously
and then on post execute do something as a result.
So once it's done executing this background process, let the user know, for example, that the execution
process is done and if it worked or it didn't work.
And what we are doing is has a background process.
I'm just using this for loop here and then on post execute.
So once the execution is done, the background process is done.
I cancel the progress dialogue and I write a little text to the user.
Just pointing out the result.
So whatever the result was that was passed to our class, execute a task called value.
And where does it come from or what does the value come from?
Well, that's the value that I had it here.
So I created this class, which just will print whatever you pass here once the execution is done.
Very basic.
Now, let's just see how this looks, that is testis on the emulator, so here we are, click for background
execution.
I'm going to click that and you see Backgrounders Xixi executed successfully.
So that's the text that I have here.
Let's run it again.
For a very short time, our custom dialogue popped up, so our custom progressed dialogue and we showed
a progress dialogue.
By the way, I didn't go into that, but we saw how progress dialogues worked.
Right.
I created this other video so you can see just the dialogue that has the content view, dialogue, custom
progress, which is just this progress bar with a text.
Please wait.
So nothing more than that.
Now let's test what happens if we do ten thousand entries in here.
So let's see how long that will go.
So let's press this button again, you can see this is a way, a longer process, so there they took
a much longer time.
And what we do with this progress bar here or with this custom progress dialogue is just to let the
user know that something is running in the background and he shouldn't do anything with the application.
But if you want to actually do something in the foreground and you don't need this custom progress dialogue,
and you can still use the application, for example, in Facebook when you scroll or an Instagram when
you scroll, it's constantly doing something in the background.
It's constantly loading the next images or the next videos for the next users.
And all this information is constantly doing that when you are scrolling through or when you're swiping
through your application.
And that is what is happening in the background.
So you don't have your Instagram app being blocked or the UI thread being blocked.
So it's constantly fluid and it works perfectly.
OK, but if you want the user to know, OK, something is happening and please wait, then such a custom
progress dialogue makes sense.
All right, once again, how do we do something in the background or how do we use an async task while
generally we need to start tracing task at one point and that's what we do here.
So I'm starting an async task and in my case, I'm just passing a value here, some text.
It's not really necessary.
But what is necessary is this execute, which will then start the execution.
By the way, in this case, it is necessary because I created the code this way.
I created this class this way.
But you can create your async task classes as you want them so they don't have to have a value, which
is just going to be text.
It could be something else.
OK, then we have our inner class private in a class execute async task which is inheriting from async.
So this is the important part here.
It has to inherit from async task.
So this is these kolon facing task.
Then inside of this async task, you have three methods on pre execute, doing background and unposed
execute.
Of course there are more methods, but these are the main methods that you will need on pre execute
is what you want to run just before the execution.
Then the doing background is what you want to do as the execution.
So what should the run in the background?
And then finally, you have the unposed executed.
So what do you want to do once the execution is done?
And I'd say let's use that in the next video where we're going to do the same thing with the bitmap.
So we're going to create the bitmap in the background.
So see there.