Click here to Skip to main content
15,879,326 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;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

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

final SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) 
findViewById(R.id.swipe_container);
pullToRefresh.setOnRefreshListener(new 
SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        readRss.execute(); 
        pullToRefresh.setRefreshing(false);
    }
});


    }
}

}

this ReadRss class :
public class ReadRss extends AsyncTask<Void, Void, Void> {
Context context;
String address = "http://karar.com/XMLFile1.xml";
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);
    progressDialog.setMessage("loading....");
}


@Override
protected void onPreExecute() {
    progressDialog.show();
    super.onPreExecute();
}


What I have tried:

this work but Idont think this is the right way :
call new ReadRss(this, recyclerView).execute();
Posted
Comments
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