2

I try to work with AsyncTaskLoader and I have one problem. I load in AsyncTaskLoader data from the internet then start other activity(BusModelsActivity) in method onLoadFinished

    @Override
       public void onLoadFinished(Loader<List<BusModelParcelable>> loader, List<BusModelParcelable> data) {
           hideDialog();
           Log.d("onLoadFinished", TestTags.TAG1);
           Intent intent = new Intent(BusSearchParamActivity.this,BusModelsActivity.class);
           intent.putParcelableArrayListExtra(AppVariables.BUS_MODELS_LIST, (ArrayList) data);
           startActivity(intent);
       }

but when I click back in activity BusModelsActivity and return to BusSearchParamActivity method onLoadFinished run again, and BusModelsActivity starts again. What to do with it? I call other activity when click at button:

   public void pickUpButtonClick(View v) {
       getSupportLoaderManager().initLoader(LOADER_MODELS,null,busModelsCallBack);
   }

where busModelsCallBack is implementation of LoaderCallbacks interface. When I did debug it stoped only on onLoadFinished, not onCreateLoader or click button.

Abbath
  • 1,002
  • 13
  • 30

2 Answers2

0

I am afraid to tell you that the AsyncTask behavior till now are not 100% granted. You have two ways to overcome this problem.

1)Do your actions in a Thread instead of AsyncTask. This will ensure that your code will be executed only once cause Thread never start twice until you want this. As following

new Thread(new Runnable() {
        @Override
        public void run() {
            /All your code here
        }
    }).start();

2)Declare a static parameter in your activity called isPageLoaded initially with value false and set it to true inside your onLoadFinished after loading your page at the first time and check on its value before executing your onLoadFinished code as following:

@Override
   public void onLoadFinished(Loader<List<BusModelParcelable>> loader, List<BusModelParcelable> data) {
       if(!isPageLoaded){
       hideDialog();
       Log.d("onLoadFinished", TestTags.TAG1);
       Intent intent = new Intent(BusSearchParamActivity.this,BusModelsActivity.class);
       intent.putParcelableArrayListExtra(AppVariables.BUS_MODELS_LIST, (ArrayList) data);
       startActivity(intent);
       isPageLoaded = true;
       }
   }
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
0

Try:

private static boolean isClicked = false;
@Override
       public void onLoadFinished(Loader<List<BusModelParcelable>> loader, List<BusModelParcelable> data) {
       if(isClicked )
       {
           hideDialog();
           Log.d("onLoadFinished", TestTags.TAG1);
           Intent intent = new Intent(BusSearchParamActivity.this,BusModelsActivity.class);
           intent.putParcelableArrayListExtra(AppVariables.BUS_MODELS_LIST, (ArrayList) data);
           startActivity(intent);
           isClicked = false;
       }
       }

public void pickUpButtonClick(View v) {
       isClicked = true;
       getSupportLoaderManager().initLoader(LOADER_MODELS,null,busModelsCallBack);
   }
Dan Cuc
  • 76
  • 4