Reputation: 6518
I am developing an application in which several fragments are involved. In each fragment I have to call web service to fetch data.
Currently I am calling web service from onCreateView() method of Fragment. Issue i am getting that whenever web service call is in progress and if device orientation is changed then new web service call starts invoking.
I think this might be because onCreateView() method gets called on configuration change.
How can I solve this. and which Life cycle method should I use to call web service so that it will be get called only once
Upvotes: 5
Views: 1698
Reputation: 6518
I have resolved this by following workaround
Create an operation identifier for each web service call method. E.g. for example "Authentication" for login call
Create one object of ArrayList say currentTasks
ArrayList<String> currentTasks = new ArrayList<String>();
In every method where I am calling web service, check if operation identifier of corresponding method is already present in ArrayList. If not then start operation.
String operationId = "Authentication";
if(currentTasks.indexOf(operationId) == -1)
{
<do web service call operation here>
currentTasks.add(operationId);
}
Method in which above operation's response is receiving, remove operation identifier from ArrayList
if(currentTasks.indexOf("Authentication") != -1){
currentTasks.remove("Authentication");
}
This will ensure that call will not go to web method which is currently in progress.
I know this is not the best way to achieve it and this might not the best practice to follow but for now this works for me.
Upvotes: 2
Reputation: 657
A simple solution would be to use a static variable pointing to asynctask. Create and call an AsyncTask if variable is not null.
Regards, Prateek
Upvotes: 0
Reputation: 5011
If your web service code is getting restarted due to a device orientation change, than you're probably using an AsyncTask
. This is a common problem with AsyncTasks and you have several ways to solve it.
One common workaround is to wrap your AsyncTask around an invisible Fragment
and make sure this fragment does not get destroyed and recreated again during an orientation change. Check out this tutorial on how to do it: Retaining Objects Across Config Changes
Upvotes: 0