Android calling AsyncTask right after an another finished
you can use getStatus()
checks whether the the AsyncTask
is pending, running, or finished.and when finsh start your new task.like:
if(authTask .getStatus() == AsyncTask.Status.PENDING){
// My AsyncTask has not started yet
}
if(authTask .getStatus() == AsyncTask.Status.RUNNING){
// My AsyncTask is currently doing work in doInBackground()
}
if(authTask .getStatus() == AsyncTask.Status.FINISHED){
// START NEW TASK HERE
}
example for your app:
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if (authTask != null && authTask.getStatus() == AsyncTask.Status.FINISHED) {
//START YOUR NEW TASK HERE
}
else
{
//IGNORE BUTTON CLICK
}
}
});
Start AsyncTask from another AsyncTask doInBackground()
According to the post below you can do Activity.runOnUiThread() to run a Runnable on the main-Thread (from another thread).
- Running code in main thread from another thread
So theoretically you could do this:
- Run the Async Task
- do Activity.runOnUiThread(Runnable) inside your AsyncTask and start a new AsyncTask from inside of this runnable
As the name says Activity.runOnUiThread() executes the runnable on the main-thread
But it's kind of hacky.
Code should look something like this: (didnt test)
// first task
(new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
ParentActitity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//second async stared within a asynctask but on the main thread
(new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
}).execute();
}
});
return null;
}
}).execute();
This nested example is not a good style to use in production because (IMHO) its close to unreadable.
Additional Notes:
Activity.runOnUiThread(Runnable) is not static! Thats why my example uses ParentActivity(.this).runOnUiThread(Runnable).
Waiting till the async task finish its work
wait until this call is finish its executing
You will need to call AsyncTask.get() method for getting result back and make wait until doInBackground
execution is not complete. but this will freeze Main UI thread if you not call get method inside a Thread.
To get result back in UI Thread start AsyncTask
as :
String str_result= new RunInBackGround().execute().get();
Related Topics
Cannot Find Symbol Databindingcomponent on Android Studio 3.2 Canary 16 Kotlin Project
How to Change the Color of the Tabs Indicator Text in Android
How to Do Page Flip/Turn/Curl Effect in Android
Enable and Disable Auto Rotate Programmatically
Connect to Wifi in Android Q Programmatically
Why Shouldn't I Use System.Out.Println() in Android
Android: How to Maximize Preferencefragment Width (Or Get Rid of Margin)
Android APK File: Re-Activate Older APK File in Google Play Store
Google Play App Description Formatting
How to Create a Number Picker Dialog
Convert Xml to JSON Object in Android
Applying Effects on Video Being Played
Anr Keydispatchingtimedout Error
How to Check Internal and External Storage If Exist
How to Open Phones Gallery Through Code