2
Hello, I have an app that so when it is accessed loads a feed, Initiating an Asynctask, but if the user presses in one of the options of Drawer before the Task ends, the app does not replace and hangs, it is progressing Spinning and nothing happens and as if it got lost.
As soon as the app opens it adds Fragment:
Fragment_Main fragmentMain = new Fragment_Main();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_main,fragmentMain,"fragmentMain");
fragmentTransaction.commit();
Where the task begins:
updateFeedTask = new UpdateFeedTask(swipeRefreshLayout, getActivity(), Fragment_Main.this);
updateFeedTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
However if the user clicks for example in the search and if the above task is not finished, nothing else happens in the app, nor crash happens.
The code below is one of the options the user can select before the task ends, the problem 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 implementation of Asynctask
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 someone help me?
Thank you
Welcome to SOPT. Without posting your code, how will we help you? Click [Edit] and add the code where you are giving the problem.
– user28595
Thank you Diego, I’ve already edited.
– Eric Rosa
@Ericrosa, you can post the implementation of your Asynctask?
– regmoraes
Posted @regmoraes
– Eric Rosa
Try to implement the onCancelled() method in your async. It is triggered when the async action is canceled, in your case by the user click
– franM