Create and Execute Asynctask in Android. aysnctask in android.
useful resource : https://youtu.be/2bQCyarmFLA
(Jo activity isse create krti h sirf vahi isse use krti h )
In most cases , it is created as a inner class inside a Activity code.
Asynctask has a main method called doinBackground() , inside which we write the code to be executed in the background.
doinBackground also has 3 sub methods that get called at different stages of its execution.
OnPreExecute() - gets executed before the doinbackground() method starts
OnProgressUpdate - keeps on executing parallel with doinbacground() method.
OnPostExecute() - gets executed after the doinBackground() gets over.
Create a asynctask and execute it below :
full code :
(You can use on asynctask object only once)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void StartMethod(View view) {
//Create a object of Asynctask class and apply the
// execute() method passing the execute method the parameters.
NestedClass nestedClass1 = new NestedClass();
nestedClass1.execute("Deepesh","Kiran","Rohan");
NestedClass nestedClass2 = new NestedClass();
nestedClass2.execute("A","B","C");
}
//Asynctask class
class NestedClass extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... strings) {
for (String name: strings) {
Log.d("Keyy", " Threadname :" + Thread.currentThread().getName()+ " ArryNames :"+ name );
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
}
[
INPUT parameter - same as the parameters taken by the doinBackground()
RESULT parameter- is the result type of the doinBckground().
]
Step 1] Create a inner class that extends to Asynctask class
Step2] Pass the Asysnctask "< >" the 3 parameter types for INPUT , PROGRESS, RESULT.
Input data type is for doInBackground() , progress data type is for OnProgressUpdate() and result data type is for onPostExecute(). The result of doInBackground() is send to onPostExecute()
Step3] Implement its doinbackground() methods and pass it the Input parameter that you'll need to take from the users (if any) .
step4 ] Write the code inside the method that you want to run in the backgorund thread
step5] Now , to execute the Asynctask , create a object of the class whihc extended the Asynctask.
and apply the execute() method on the object , pass the parameters required inside the execute() method.
You can execute the Asynctask multiple times by creating multiple objects of DIFFERENT NAMES, whihc will run SEQUENCIALLY one after another.
EXAMPLE 2 :
static class NestedClass extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... strings) {
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
}
--------------------------------------------------------------------------------------------------------------
OnPreExecute() - runs on the Main thread or on the thread that STARTS the Asynctask.
OnProgressUpdate & OnPostexecute - runs on UI thread.
doinBackground() - runs on the BACKGROUND thread.
YOU CANNOT ACESS THE MAIN / UI THREAD FROM ASYNCTASK DIRECLTY
1] Asynctask doesnt survive Orientation changes, hence we use AsynctaskLoader
---------------------------------------------------------------
How to update UI from asynctask :
https://code8808.blogspot.com/2020/07/update-ui-from-asynctask-using.html
Problems with Asynctask :
https://youtu.be/hcUIhSVYKiI

Comments
Post a Comment