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();