Activate AsyncTask after surgery

I am developing an Android 3.1 application.

I want to execute AsyncTaskafter displaying activity. I want to show something to the user before executing AsyncTask.

I read that it is not recommended to run AsyncTaskon onCreate().

Where do I need to run AsyncTaskon onStart()or onResume()?

I want to leave enough time to show activity before doing it.

+3
source share
4 answers

onCreate(), onStart()and onResume()are life cycle methods called by the operating system and should not be called directly. However, you can override them so that your code executes at these stages of the life cycle of actions:

enter image description here

, , AsyncTask , View , :

    toReturn.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        public void onGlobalLayout() {
            toReturn.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            // asyncTask.execute();
        }
    });

toReturn - onCreate(). .

ViewTreeObserver , , . , removeGlobalOnLayoutListener() `, , .

+10

onResume()

, . , AsyncTask .

.

, , onCreate(), empty arraylist listview, onResume() Asynctask, ArrayList adapter.notifyDataSetChanged()

... , , ..

, - if(arrayList.size()==0), asynctask else dont.

+1

You can put yur code in the onWindowsFocusChanged method. You can use the thread inside it to control the timer to run your specific asynthesis. Keep in mind that this will be done every time your activity is concentrated not only the first time you start your activity (I don’t know if this may be a problem for you).

0
source

implement the object Viewand override onDraw().
this way you will know exactly when the first screen will be visible to the user

-2
source

All Articles