Once the app is open it adds the fragment:
Fragment_Main fragmentMain = new Fragment_Main();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_main,fragmentMain,"fragmentMain");
fragmentTransaction.commit();
Where to start the task:
updateFeedTask = new UpdateFeedTask(swipeRefreshLayout, getActivity(), Fragment_Main.this);
updateFeedTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
But if the user clicks for example on the search and if the above task is not yet finished, nothing else happens in the app, nor crash happens.
The code below is one of the options that the user can select before the task is finished, the problem is happening:
searchView.setOnSearchClickListener(new View.OnClickListener()
@SuppressWarnings("StatementWithEmptyBody")
@Override
public void onClick(View v) {
if(Fragment_Main.updateFeedTask != null) {
Fragment_Main.updateFeedTask.cancel(true);
}
Fragment fragment = getSupportFragmentManager().findFragmentByTag("fragmentMain");
if (fragment != null){
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
}
Fragment_SearchActivity fragmentSearchActivity = Fragment_SearchActivity.newInstance(searchView);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_main, fragmentSearchActivity, "fragmentSearchActivity");
ft.addToBackStack("back");
ft.commit();
if (!isDrawerLocked) {
drawerLayout.closeDrawer(drawerListOptions);
}
}
});
Below the AsyncTask implementation
public class UpdateFeedTask extends AsyncTask<Void, Void, FeedCardAdapter> {
private SwipeRefreshLayout swipeRefreshLayout;
private Activity activity;
private DelegateFeed delegateFeed;
public UpdateFeedTask(SwipeRefreshLayout swipeRefreshLayout, Activity activity, DelegateFeed delegateFeed) {
this.swipeRefreshLayout = swipeRefreshLayout;
this.activity = activity;
this.delegateFeed = delegateFeed;
}
@Override
protected void onPreExecute() {
swipeRefreshLayout.setRefreshing(true);
}
@Override
protected FeedCardAdapter doInBackground(Void... params) {
List<Obj> objList = new ObjDAO().getMyFeed();
return new FeedCardAdapter(objList , R.layout.feed, activity, delegateFeed);
}
@Override
protected void onPostExecute(FeedCardAdapter feedCardAdapter) {
delegateFeed.setFeedCardAdapter(feedCardAdapter);
}
}
Could anyone help me?
Thank you