Android - Unable to complete task: task has already been completed
My async task call is inside the onCreate () method of my activity
my_random_task = new MyRandomTask();
my_random_task.setListener(this);
final Button post_btn = (Button) findViewById(R.id.post_btn);
post_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText post_text = (EditText) findViewById(R.id.post_text);
if (!post_text.isEmpty()) {
summoner_search_task.execute(summoner_name);
}else{
Toast.makeText(getApplicationContext(), "Please type a post text.", Toast.LENGTH_SHORT).show();
post_text.requestFocus();
}
}
});
The async is empty, but when I try to invoke the asynthesis again by clicking the button, I get
java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
Is there a way to make the asynthesis reset and run it again after clicking the button?
+3
source to share
1 answer
I was passing in a CallBackListener interface that included a callback function that was triggered when the async task finished.
So what I did (thanks to @TronicZomB and @stkent):
final CallBackListener cbl = this;
final Button post_btn = (Button) findViewById(R.id.post_btn);
post_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText post_text = (EditText) findViewById(R.id.post_text);
if (!post_text.isEmpty()) {
my_random_task = new MyRandomTask();
my_random_task.setListener(cbl);
summoner_search_task.execute(summoner_name);
}else{
Toast.makeText(getApplicationContext(), "Please type a post text.", Toast.LENGTH_SHORT).show();
post_text.requestFocus();
}
}
});
0
source to share