android


Is it possible to call doInBackground from onPostExecute?


#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
asyntask.execute();
}
I'm reading data from some API. Is it possible to call doInBackground() from onPostExecute?
I want do it recursively like (network task and update in UI ) for 5 times. Thanks in advance.
Starting the AsyncTask again from inside onPostExecute is a horrible idea. As you want to do it recursively like 5 times for network calls along with UI update, I would like to suggest you to keep an interface to keep track of the AsyncTask call.
So here's an example about how you can achieve the behaviour. You may create an interface like this.
public interface MyResponseListener {
void myResponseReceiver(String result);
}
Now you declare the interface in your AsyncTask class too. So your AsyncTask may look like this.
public class YourAsyncTask extends AsyncTask<Void, Void, String> {
// Declare an interface
public MyResponseListener myResponse;
// Now in your onPostExecute
#Override
protected void onPostExecute(final String result) {
// Send something back to the calling Activity like this to let it know the AsyncTask has finished.
myResponse.myResponseReceiver(result);
}
}
Now you need to implement the interface you've created already in your Activity like this. You need to pass the reference of the interface to the AsyncTask you're starting from your Activity
public class MainActivity extends Activity implements MyResponseListener {
// Your onCreate and other function goes here
// Declare an AsyncTask variable first
private YourAsyncTask mYourAsyncTask;
// Here's a function to start the AsyncTask
private startAsyncTask(){
mYourAsyncTask.myResponse = this;
// Now start the AsyncTask
mYourAsyncTask.execute();
}
// You need to implement the function of your interface
#Override
public void myResponseReceiver(String result) {
if(!result.equals("5")) {
// You need to keep track here how many times the AsyncTask has been executed.
startAsyncTask();
}
}
}
doInBackground() is called before calling onPostExecute.
Since you cannot perform network tasks on the UI thread, an AsyncTask is created.
The AsyncTask executes in the background, performing the network task on the worker thread. Then after the background task is completed, onPostExecute() is called that makes the UI changes on the UI thread.
This should help : https://developer.android.com/reference/android/os/AsyncTask.html
https://developer.android.com/training/basics/network-ops/index.html
The AsyncTask class is used to do some work in background and publish the results to the MainThread so it's generally not possible because the work that's being done in the worker thread may not be doable in MainThread, (like the NetworkOnMainThreadException when you do networking in MainThread).
I suggest you to make an array of your jobs and call the execute() method of your subclass of AsyncTask and it will serialize the jobs to be done in a worker thread.

Related Links

Android: Improve quality for screen recording by the use of API MEDIA_PROJECTION_SERVICE
Peer not authenticated in AndroidStudio, but not on commandline-gradle
White text under buttons in layout
How can I access a component(for example a timepicker) in one activity from another activity in Android?
Google play shows error code 504 for Android N preview
How to load image from writeable folder and assign to ImageView in Android?
EditText updates items on ListView with TextWatcher
FTDriver.java mDevice null error
How to use Android NavigationDrawer to create other fragments?
Android Studio resource not found after duplicate
ListView scroll position jumps to first element after notifydatasetchanged
Finger print lock in Android
Why doesn't my BTLE virtual beacon show in Chrome?
Android View Id's
Xamarin Costs to Publish on Google Play Store
UnsupportedOperationException on TextInputLayout when type count exceeded counterMaxLength

Categories

HOME
service-worker
oracle12c
deep-learning
telegram
phpmyadmin
nunit
youtube-data-api-v3
sublimetext
grafana
access
edge
aws-cli
hpc
echo
vert.x
webix
nfs
fabric.io
plist
android-securityexception
code-generation
heat
mongodb-csharp
microsoft-dynamics-nav
null-pointer
intershop
vuejs
sencha-touch
google-maps-autocomplete
object-storage
scriptella
gmock
perlbrew
dapper-extensions
xcode-ui-testing
slidetoggle
nstimer
apktool
linked-data
spotipy
nsurl
weather
cart
playstation-portable
papaparse
hard-drive
contacts
html-encode
health-monitoring
demo
signals-slots
dcmtk
install.packages
drupal-theming
monkey-testing
plan-9
dtsearch
tinymce-3
mongotemplate
git-flow
jqchart
java-websocket
d3-force-directed
rxtx
latin
uicolor
nikeplus-api
ng-grid
embedding
cassia
gcloud-ruby
boost-serialization
phpwebsocket
redis-cluster
upx
kendo-upload
ascii-art
jbase
boomerang
vigenere
xvim
omnifaces
umlgraph
mmwormhole
beatsmusic
browserify-shim
rickshaw
with-statement
ray
vbe
spring-3
mvc-editor-templates
time.h
pclzip
video-codecs
paginator
lambdaj
oracle-enterprise-linux
predicatebuilder
hibernateexception
luabind
listactivity
webdev.webserver
tournament
dataform
socketserver
load-time
instantiationexception
dot-emacs
rowlex
echo3
glui
movieplayer
pbcopy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App