Click here to Skip to main content
15,125,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Activity I use a class which extends from AsyncTask and a parameter which is an instance of that AsyncTask. When I call readRss.execute() everything is fine. But the app crash when I want to update which calls again the readRss.execute(). Cause then appears an Exception which says :
Cannot execute task: the task has already been executed (a task can be executed only once)

I don't want to create new instances of the Asyntask.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
protected void onCreate(Bundle savedInstanceState) {
    recyclerView = (RecyclerView) findViewById(;

    final ReadRss readRss = new ReadRss(this, recyclerView);

final SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) 
SwipeRefreshLayout.OnRefreshListener() {
    public void onRefresh() {



this ReadRss class :
public class ReadRss extends AsyncTask<Void, Void, Void> {
Context context;
String address = "";
ProgressDialog progressDialog;
ArrayList<FeedItem> feedItems;
RecyclerView recyclerView;
URL url;

public ReadRss(Context context, RecyclerView recyclerView) {
    this.recyclerView = recyclerView;
    this.context = context;
    progressDialog = new ProgressDialog(context);

protected void onPreExecute() {;

What I have tried:

this work but Idont think this is the right way :
call new ReadRss(this, recyclerView).execute();
David Crow 10-Aug-18 9:46am
"but Idont think this is the right way"

Why not?
Mike V Baker 10-Aug-18 9:48am
Why don't you want to create new instances of the AsyncTask? I believe the right way to do it is to create an AsyncTask, use it, let it go out of scope and get cleaned.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900