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

Why is Keyguard not being Re-enabled?
Is there a way to get all location point (lat and lon) around a specific country?
How to set Socket timeout of fail connecting
how can decode json data form api in android?
Disable SQLiteLog in Android
http-link to private Dropbox files
Mvvmcross Loading Indicator not showing
VideoView problems in ScrolView
Android app publishing/ google play store app publication query
Php Post FATAL EXCEPTION — Async and permission included
No resource identifier found for attribute 'isSelected' in package 'auto'
XMPP error using asmack
Android - Graying out/changing the alpha of all items aside from the selected item
How to bring ImageView in front of Button in android 5?
How to set time by location Android
Shell script to extract image of an Android mobile

Categories

HOME
batch-file
xml
gridview
fuelux
c++11
nvd3.js
phpmyadmin
character-encoding
3d
yahoo-finance
grafana
angularfire2
crystal-lang
arangodb
fabric.io
augeas
mxgraph
line
ycsb
angular-dart
endpoint
nancy
3nf
heat
searchview
xmonad
connector
babel
imagemap
question2answer
inno-download-plugin
beta
android-sharedpreferences
autodesk-designautomation
git-squash
hreflang
android-gridview
activexobject
indy10
searchkit
apic
reason
loading
visa
skylink
softmax
naming
show-hide
flask-restful
httrack
dotnet-httpclient
mschart
fclose
tinymce-3
java-6
type-theory
mouseleave
choice
seek
model-associations
http-status-code-401
kendo-upload
httpruntime.cache
toran-proxy
rate-limiting
dock
pax-exam
pyobjc
telepat
mmwormhole
openbabel
primitive-types
android-vibration
ghcjs
cocos3d
nsmenu
cancellation
clickbank
conditionaltagsupport
code-duplication
flex++
mysql-error-2013
preload
intersect
curb
onmousemove
dajaxice
garbage
jqote
quotation-marks
cdonts
diagrams
ruby-1.8
updatesourcetrigger
easyb
dot-emacs
request-headers
django-piston
wcf-instancing
dsl-tools

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