22

E.g.: you gonna do something that will take a few seconds and don't wanna freeze your UI thred, right? You could use an AsyncTask but you don't wanna create a external (or inner) class to solve a small freeze problem.

So, is a good pratice do it?

package com.example.stackoverflowsandbox;

import android.os.AsyncTask;

public class Foo {
    // E.g. before call foo method you change you Activity to loading state.
    private void foo() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground( final Void ... params ) {
                // something you know that will take a few seconds

                return null;
            }

            @Override
            protected void onPostExecute( final Void result ) {
                // continue what you are doing...

                Foo.this.continueSomething();
            }
        }.execute();
    }

    private void continueSomething() {
        // some code...
    }
}

I've faced with that when I compressing Bitmaps and looping big array to update some data inside items.

Idemax
  • 2,712
  • 6
  • 33
  • 66
  • 3
    I dont see how this saves any work vs creating an inner class, just looks terribly messy IMO – tyczj Jul 18 '14 at 14:26
  • 1
    It depends... the only thing what could happen is that the Thread(AsyncTask) keeps running after the Activity was put in the background or is destroyed. But in general this is awful, because the anonymous class is borrowed somewhere in the code. – Steve Benett Jul 18 '14 at 14:28
  • 2
    just for a question of readability, remember that objects create from anonymous inner class are called with the notation `OuterClass$2`, and if you start nesting inner class, the debuggin can be difficult – Blackbelt Jul 18 '14 at 14:28
  • 1
    I do not see any problems, but in some cases you might prefer to create a external definition for reuse purposes. I personally prefer the external asynctasks/listeners pattern. There is a performance tip about creating private inner classes http://developer.android.com/training/articles/perf-tips.html#PackageInner that does not apply to your case, but it worth a read. – tato.rodrigo Jul 18 '14 at 14:45

1 Answers1

36

Yes, but not the way you do it.

Remember that starting Honeycomb the default execution model of AsyncTasks is serial:

  new AsyncTask<Void, Void, Void>() {
         ....
         ....
  }.execute(); <------ serial execution


Instead use a thread pool executor:

  new AsyncTask<Void, Void, Void>() {
         ....
         ....
  }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null); <------ parallel execution
Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30
  • Thanks but I mean if is a good practice code like that. =] – Idemax Jul 18 '14 at 14:42
  • if by 'like that' you mean instantiation of anonymous asynctasks then the answer is yes. no problems in terms of code execution and even a starting android developer will know what you meant – Gilad Haimov Jul 18 '14 at 14:44
  • 11
    CLARIFICATION: execute() runs tasks serially only with respect to other AsyncTasks invoked that way. It's a single background thread that runs them, but it's still parallel wrt the caller. – Slawomir May 08 '15 at 12:15
  • I am not convinced that you always want to do things in parallel in the background. Serial is often easier and still doesn't freeze the UI. – JohnyTex Jul 06 '16 at 09:49
  • 3
    "Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution." – JohnyTex Jul 06 '16 at 10:32
  • See [the doc](https://developer.android.com/reference/android/os/AsyncTask#order-of-execution) for JohnyTex citation, this answer is not really advised by Android ! – Dan Chaltiel May 13 '18 at 08:14