Reputation: 915
In my application I need to call two web-services to fetch the records from server on app start. I did that in Asynch Task and on splash screen but its not running fine. It waits for the aynsch task to complete his operation first then its moving to next activity.
I want my app should call webservice in background on app startup without blocking the user to move next activity.
any help would be appreciated.
Upvotes: 0
Views: 1529
Reputation: 24848
**call Asyn server call wherever you need**
getServerResponse();
"startnewactivity" here
finish current activity here
**Asyn server call**
public void getServerResponse(){
new AsyncTask<Void, Void, Object>() {
@Override
protected Object doInBackground(Void... params) {
//write your server response code here
return null;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
}
}.execute();
}
Upvotes: 1
Reputation: 7082
Using an Async task you can run the web service call in a background thread and your UI thread wouldn't be blocked and you can call the next activity in background using the runonuithread but it makes seamless solution. So its better to wait finish the background process first
Upvotes: 0
Reputation: 4275
You can use an IntentService and fetch the details and store it.
Or You can use a Service, bind it with the activity and send the data when you have retrieved it.
Additionally you can navigate to the 2nd activity and call the AsyncTask to fetch the details and update the UI when done.
If the data retrieved is global and used by multiple activities then store it in the Application class and add a stateChangeListener and get notified when its updated from a background service or AsyncTask.
Upvotes: 0