Issue
I am developing an app where I am loading images from gallery. I saw my GUI freezing a little bit and a message saying "Skipped 53 frames. Your app might be doing huge work in its main thread" something like that. Upon a little bit of research I found that it is a common issue and can be resolved using Async task etc. I have defined a method in my main activity and I am calling that method at the very beginning of my activity, that method must be doing a lot of work. I want to call that method in an Async task or on a parallel thread which will run parallel to my main thread and until it is finished I want to display a message box with the text "processing...." on my GUI. How can I achieve this. I am not passing any arguments to the method in my method call nor I am returning anything from that method.
Solution
you could use RxJava if you are using Java and Coroutines if you are using Kotlin they are the most optimal solutions at present. But if you still want to use an Async task here is the way.
create an inner class like this,
private class AsyncTaskExample extends AsyncTask<null, null, null> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//show progress bar or any
thing you want to do before starting the process
}
@Override
protected void doInBackground() {
//your code that gets the images from gallery
}
@Override
protected void onPostExecute() {
super.onPostExecute();
// use this callback for if you want to stop showing the progress bar or anything that you want to do after the executing your code in Backgroud
}
} }
then call this by doing this whenever you want to execute your code,
AsyncTaskExample asyncTask=new AsyncTaskExample();
asyncTask.execute();
Answered By - Nitin Tej
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.