6

I have a nested fragment containing the following method:

public void onSave() {
    if (getActivity() == null || view == null) return;
    if (file != null && file.exists()) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected void onPreExecute() {
                Log.d("log", "onPreExecute of save ex");
            }

            @Override
            protected Void doInBackground(Void... params) {

                Log.d("log", "doInBackground of save ex");
                //DO SOMETHING
                return null;
            }

            protected void onPostExecute(Void result) {
                BaseFragment fragment = new LocalListFragment();
                ((LocalLauncherFragment)(LocalEditFragment.this.getParentFragment())).setFragment(fragment);
                Log.d("log", "end of save ex");
            };
        }.execute();
    } else {
        showAlert();
    }
}

My problem is that if I call this method for the first time, it executes until onPostExecute(). However if I go to other fragment and opens this fragment newly(by creating a new fragment object and replacing to it) then only onPreExecute() is executed. Why doesn't this asyncTask object get executed well for the second time?

Intestingly if I use executeOnExecutor() then it works fine for the second time. But why doesn't execute() work? Is AsyncTask's life tied to fragment or something?

Thanks in advance!

user2062024
  • 3,541
  • 7
  • 33
  • 44
  • I'm not definite on this, but Fragments can have UI components, so I'd be careful about messing with them much in background threads. Have you tried putting your log statement as the first line in the `doInBackground` method and see if it shows up? – kabuko Aug 21 '13 at 20:44
  • yea i did. on the first try, both onPreExecute() doInBackground() are called but on the second time, only onPReExeucte() is called :( – user2062024 Aug 21 '13 at 21:19
  • When you're using `executeOnExecutor` I'm guessing you're using `THREAD_POOL_EXECUTOR` and not `SERIAL_EXECUTOR` right? Try with `SERIAL_EXECUTOR` and I'd wager you'll get the same (unwanted) behavior. – kabuko Aug 21 '13 at 22:32
  • Yea, using SERIAL_EXECUTOR gives me the same result as using execute(). Something seems really hanging on the AsyncTask thread but cannot figure out why... because on the first run, onPostExecute() gets called without any problema! – user2062024 Aug 22 '13 at 17:48

2 Answers2

1

It sounds to me like something is hanging in the AsyncTask. In modern versions of Android AsyncTasks run on a single thread unless you specify an Executor that's multi-threaded. onPreExecute() still runs successfully because that runs on the main thread. You never see doInBackground the second time though because the single background thread is still hung from the first call. You'll need to examine the contents of LocalKeekLauncherFragment.setFragment(fragment) to see what's causing the hang.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • Actually those 2 lines of fragment transition was supposed to be in onPostExecute(). I edited code code. And still... on the first try, onPreExecute(), doInBackgroiund(), onPostExecute() run. on the second try, only onPreExecute() runs.. If onPostExecute() runs on the first try, it means AsyncTask didn't hang on the first try, doesn't it? – user2062024 Aug 21 '13 at 23:42
  • I agree with you that my AsyncTask is blocked because I found out that after I ran that the second time, all other parts where AsyncTask.execute() stopped working. – user2062024 Aug 21 '13 at 23:43
  • I checked the status of asyncTask before executing it and it seems fine on the second try.. – user2062024 Aug 21 '13 at 23:54
  • That's a good point about `onPostExecute`, but my suspicion is still that something is causing this hang in the background thread. Can you post the code for `//DO SOMETHING`? – kabuko Aug 22 '13 at 17:36
0

It seems that doInBackground thread has probably crashed second time.It cannot remain stuck from first try as onPostExecute get called and this is only possible if doInBackground has returned value successfully.

Shinoo Goyal
  • 601
  • 8
  • 10