How one interface can be used for more than two background android tasks within one activity?How can I concatenate two arrays in Java?How do save an Android Activity state using save instance state?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I pass data between Activities in Android application?How do I create a transparent Activity on Android?Is AsyncTask really conceptually flawed or am I just missing something?Android - Cancel AsyncTask ForcefullyAndroid - Start a new activity while the application is in the backgroundHow to differentiate two data sets returned by the same custom interface methodWhy AsyncTask is not working?
On The Origin of Dissonant Chords
"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?
a sore throat vs a strep throat vs strep throat
can anyone help me with this awful query plan?
How could Tony Stark make this in Endgame?
Minor Revision with suggestion of an alternative proof by reviewer
Pulling the rope with one hand is as heavy as with two hands?
Are there physical dangers to preparing a prepared piano?
Implications of cigar-shaped bodies having rings?
Two field separators (colon and space) in awk
Read line from file and process something
"You've called the wrong number" or "You called the wrong number"
How to fry ground beef so it is well-browned
Is it idiomatic to construct against `this`
Could the terminal length of components like resistors be reduced?
How much cash can I safely carry into the USA and avoid civil forfeiture?
How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?
What's the polite way to say "I need to urinate"?
As an international instructor, should I openly talk about my accent?
How to denote matrix elements succinctly?
Multiple options vs single option UI
How to write a column outside the braces in a matrix?
Is Diceware more secure than a long passphrase?
Map of water taps to fill bottles
How one interface can be used for more than two background android tasks within one activity?
How can I concatenate two arrays in Java?How do save an Android Activity state using save instance state?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I pass data between Activities in Android application?How do I create a transparent Activity on Android?Is AsyncTask really conceptually flawed or am I just missing something?Android - Cancel AsyncTask ForcefullyAndroid - Start a new activity while the application is in the backgroundHow to differentiate two data sets returned by the same custom interface methodWhy AsyncTask is not working?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an activity class which is calling background AsyncTask twice like
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params);
bt.execute(url2);
one is used to GET the data and another is used to POST the data to server.
Constructors of this AsyncTask is as follows
public BackgroundTask(Context context, String method)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
public BackgroundTask(Context context, String method, ArrayList<NameValuePair> params)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
this.params = params;
Now MainActivity.class is implemented by onBackgroundTaskListener<String> interface as
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result);
onPostExecute of AsyncTask class, String result is returned or passed back to the calling class, shown below.
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Now I have one method (onTaskComplete) in MainActivity to tackle two responses coming from one background task. I would like to use the condition to figure out which execution is returned a result like...
@Override
public void onTaskComplete(String result)
if (execution == 1)
// Parse the GET result
else if(execution == 2)
// Parse the POST result
I don't want to implement the multiple AsyncTask and would like to achieve it using single AsyncTask and called this AsyncTask multiple times within single activity. What should be the possible way to implement this.
java
add a comment |
I have an activity class which is calling background AsyncTask twice like
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params);
bt.execute(url2);
one is used to GET the data and another is used to POST the data to server.
Constructors of this AsyncTask is as follows
public BackgroundTask(Context context, String method)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
public BackgroundTask(Context context, String method, ArrayList<NameValuePair> params)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
this.params = params;
Now MainActivity.class is implemented by onBackgroundTaskListener<String> interface as
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result);
onPostExecute of AsyncTask class, String result is returned or passed back to the calling class, shown below.
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Now I have one method (onTaskComplete) in MainActivity to tackle two responses coming from one background task. I would like to use the condition to figure out which execution is returned a result like...
@Override
public void onTaskComplete(String result)
if (execution == 1)
// Parse the GET result
else if(execution == 2)
// Parse the POST result
I don't want to implement the multiple AsyncTask and would like to achieve it using single AsyncTask and called this AsyncTask multiple times within single activity. What should be the possible way to implement this.
java
add a comment |
I have an activity class which is calling background AsyncTask twice like
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params);
bt.execute(url2);
one is used to GET the data and another is used to POST the data to server.
Constructors of this AsyncTask is as follows
public BackgroundTask(Context context, String method)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
public BackgroundTask(Context context, String method, ArrayList<NameValuePair> params)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
this.params = params;
Now MainActivity.class is implemented by onBackgroundTaskListener<String> interface as
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result);
onPostExecute of AsyncTask class, String result is returned or passed back to the calling class, shown below.
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Now I have one method (onTaskComplete) in MainActivity to tackle two responses coming from one background task. I would like to use the condition to figure out which execution is returned a result like...
@Override
public void onTaskComplete(String result)
if (execution == 1)
// Parse the GET result
else if(execution == 2)
// Parse the POST result
I don't want to implement the multiple AsyncTask and would like to achieve it using single AsyncTask and called this AsyncTask multiple times within single activity. What should be the possible way to implement this.
java
I have an activity class which is calling background AsyncTask twice like
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params);
bt.execute(url2);
one is used to GET the data and another is used to POST the data to server.
Constructors of this AsyncTask is as follows
public BackgroundTask(Context context, String method)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
public BackgroundTask(Context context, String method, ArrayList<NameValuePair> params)
this.context = context;
this.method = method;
this.callback = (onBackgroundTaskListener<String>) context;
this.params = params;
Now MainActivity.class is implemented by onBackgroundTaskListener<String> interface as
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result);
onPostExecute of AsyncTask class, String result is returned or passed back to the calling class, shown below.
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Now I have one method (onTaskComplete) in MainActivity to tackle two responses coming from one background task. I would like to use the condition to figure out which execution is returned a result like...
@Override
public void onTaskComplete(String result)
if (execution == 1)
// Parse the GET result
else if(execution == 2)
// Parse the POST result
I don't want to implement the multiple AsyncTask and would like to achieve it using single AsyncTask and called this AsyncTask multiple times within single activity. What should be the possible way to implement this.
java
java
asked Mar 22 at 17:28
rpurantrpurant
100113
100113
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
enum BackgroundType GET, POST
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result, BackgroundType type);
--------------------------
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
bt.execute(url2);
------------------------
@Override
public void onTaskComplete(String result, BackgroundType type)
switch(type)
case GET:
break;
case POST:
break;
add a comment |
I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask
Then the new class can then encapsulate the string you require along with other variables you would need.
For example:
// Create a new class to hold the result of `AsyncTask` execution
public class TaskExecutionResult
public String method;
public String resultString;
// When subclassing AsyncType assign TaskExecutionResult as return type
// Please replace URL with input parameters you require (perhaps a method string)
private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>
// doInBackground will now return TaskExecutionResult type of object
protected TaskExecutionResult doInBackground(URL... urls)
// Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
...
// onPostExecute will also accept TaskExecutionResult type of object
protected void onPostExecute(TaskExecutionResult result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Then use TaskExecutionResult in onTaskComplete:
public void onTaskComplete(TaskExecutionResult result)
if (result.method.equals("GET"))
// Parse the GET result (from result.resultString)
else if(result.method.equals("POST"))
// Parse the POST result (from result.resultString)
This approach will also allow you to easily update the result object if you needed to add more return variables.
could you please elaborate more. Could not understand what you want say.
– rpurant
Mar 23 at 5:45
@rpurant, I've added an example. Hope it helps
– foobar
Mar 24 at 10:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304926%2fhow-one-interface-can-be-used-for-more-than-two-background-android-tasks-within%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
enum BackgroundType GET, POST
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result, BackgroundType type);
--------------------------
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
bt.execute(url2);
------------------------
@Override
public void onTaskComplete(String result, BackgroundType type)
switch(type)
case GET:
break;
case POST:
break;
add a comment |
enum BackgroundType GET, POST
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result, BackgroundType type);
--------------------------
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
bt.execute(url2);
------------------------
@Override
public void onTaskComplete(String result, BackgroundType type)
switch(type)
case GET:
break;
case POST:
break;
add a comment |
enum BackgroundType GET, POST
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result, BackgroundType type);
--------------------------
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
bt.execute(url2);
------------------------
@Override
public void onTaskComplete(String result, BackgroundType type)
switch(type)
case GET:
break;
case POST:
break;
enum BackgroundType GET, POST
public interface onBackgroundTaskListener<T>
void onTaskComplete(T result, BackgroundType type);
--------------------------
BackgroundTask bt1 = new BackgroundTask(this, ApiHelper.GET, GET);
bt.execute(url1);
BackgroundTask bt2 = new BackgroundTask(this, ApiHelper.POST, params, POST);
bt.execute(url2);
------------------------
@Override
public void onTaskComplete(String result, BackgroundType type)
switch(type)
case GET:
break;
case POST:
break;
answered Mar 22 at 17:38
BahmanBahman
1,3012717
1,3012717
add a comment |
add a comment |
I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask
Then the new class can then encapsulate the string you require along with other variables you would need.
For example:
// Create a new class to hold the result of `AsyncTask` execution
public class TaskExecutionResult
public String method;
public String resultString;
// When subclassing AsyncType assign TaskExecutionResult as return type
// Please replace URL with input parameters you require (perhaps a method string)
private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>
// doInBackground will now return TaskExecutionResult type of object
protected TaskExecutionResult doInBackground(URL... urls)
// Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
...
// onPostExecute will also accept TaskExecutionResult type of object
protected void onPostExecute(TaskExecutionResult result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Then use TaskExecutionResult in onTaskComplete:
public void onTaskComplete(TaskExecutionResult result)
if (result.method.equals("GET"))
// Parse the GET result (from result.resultString)
else if(result.method.equals("POST"))
// Parse the POST result (from result.resultString)
This approach will also allow you to easily update the result object if you needed to add more return variables.
could you please elaborate more. Could not understand what you want say.
– rpurant
Mar 23 at 5:45
@rpurant, I've added an example. Hope it helps
– foobar
Mar 24 at 10:44
add a comment |
I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask
Then the new class can then encapsulate the string you require along with other variables you would need.
For example:
// Create a new class to hold the result of `AsyncTask` execution
public class TaskExecutionResult
public String method;
public String resultString;
// When subclassing AsyncType assign TaskExecutionResult as return type
// Please replace URL with input parameters you require (perhaps a method string)
private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>
// doInBackground will now return TaskExecutionResult type of object
protected TaskExecutionResult doInBackground(URL... urls)
// Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
...
// onPostExecute will also accept TaskExecutionResult type of object
protected void onPostExecute(TaskExecutionResult result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Then use TaskExecutionResult in onTaskComplete:
public void onTaskComplete(TaskExecutionResult result)
if (result.method.equals("GET"))
// Parse the GET result (from result.resultString)
else if(result.method.equals("POST"))
// Parse the POST result (from result.resultString)
This approach will also allow you to easily update the result object if you needed to add more return variables.
could you please elaborate more. Could not understand what you want say.
– rpurant
Mar 23 at 5:45
@rpurant, I've added an example. Hope it helps
– foobar
Mar 24 at 10:44
add a comment |
I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask
Then the new class can then encapsulate the string you require along with other variables you would need.
For example:
// Create a new class to hold the result of `AsyncTask` execution
public class TaskExecutionResult
public String method;
public String resultString;
// When subclassing AsyncType assign TaskExecutionResult as return type
// Please replace URL with input parameters you require (perhaps a method string)
private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>
// doInBackground will now return TaskExecutionResult type of object
protected TaskExecutionResult doInBackground(URL... urls)
// Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
...
// onPostExecute will also accept TaskExecutionResult type of object
protected void onPostExecute(TaskExecutionResult result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Then use TaskExecutionResult in onTaskComplete:
public void onTaskComplete(TaskExecutionResult result)
if (result.method.equals("GET"))
// Parse the GET result (from result.resultString)
else if(result.method.equals("POST"))
// Parse the POST result (from result.resultString)
This approach will also allow you to easily update the result object if you needed to add more return variables.
I would recommend you to create a new class and use this class instead of String when creating AsyncTask and communicating result back from the AsyncTask
Then the new class can then encapsulate the string you require along with other variables you would need.
For example:
// Create a new class to hold the result of `AsyncTask` execution
public class TaskExecutionResult
public String method;
public String resultString;
// When subclassing AsyncType assign TaskExecutionResult as return type
// Please replace URL with input parameters you require (perhaps a method string)
private class BackgroundTask extends AsyncTask<URL, Integer, TaskExecutionResult>
// doInBackground will now return TaskExecutionResult type of object
protected TaskExecutionResult doInBackground(URL... urls)
// Here populate a value which will identify your AsyncTask into return value (perhaps a method string)
...
// onPostExecute will also accept TaskExecutionResult type of object
protected void onPostExecute(TaskExecutionResult result)
super.onPostExecute(result);
callback.onTaskComplete(result);
Then use TaskExecutionResult in onTaskComplete:
public void onTaskComplete(TaskExecutionResult result)
if (result.method.equals("GET"))
// Parse the GET result (from result.resultString)
else if(result.method.equals("POST"))
// Parse the POST result (from result.resultString)
This approach will also allow you to easily update the result object if you needed to add more return variables.
edited Mar 24 at 10:51
answered Mar 22 at 17:43
foobarfoobar
435814
435814
could you please elaborate more. Could not understand what you want say.
– rpurant
Mar 23 at 5:45
@rpurant, I've added an example. Hope it helps
– foobar
Mar 24 at 10:44
add a comment |
could you please elaborate more. Could not understand what you want say.
– rpurant
Mar 23 at 5:45
@rpurant, I've added an example. Hope it helps
– foobar
Mar 24 at 10:44
could you please elaborate more. Could not understand what you want say.
– rpurant
Mar 23 at 5:45
could you please elaborate more. Could not understand what you want say.
– rpurant
Mar 23 at 5:45
@rpurant, I've added an example. Hope it helps
– foobar
Mar 24 at 10:44
@rpurant, I've added an example. Hope it helps
– foobar
Mar 24 at 10:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304926%2fhow-one-interface-can-be-used-for-more-than-two-background-android-tasks-within%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown