Is a good practice create anonymous AsyncTask for parallel small known freeze process? [closed]
Asked Answered
R

1

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.

Respire answered 18/7, 2014 at 14:23 Comment(4)
I dont see how this saves any work vs creating an inner class, just looks terribly messy IMONuncupative
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.Alenaalene
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 difficultBeltane
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 developer.android.com/training/articles/… that does not apply to your case, but it worth a read.Flocculent
C
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
Commerce answered 18/7, 2014 at 14:34 Comment(6)
Thanks but I mean if is a good practice code like that. =]Respire
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 meantCommerce
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.Danicadanice
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.Graduate
"Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution."Graduate
See the doc for JohnyTex citation, this answer is not really advised by Android !Cannery

© 2022 - 2024 — McMap. All rights reserved.