Long Operations on Activity onCreate ()

I have an Activity that retrieves information from a remote server and displays it in a TableLayout. A function that retrieves information from the server has its own timeout, and an exception is thrown when the timeout is exceeded.

Now that the activity is loaded, I want the function to be launched, and the progressDialog indicator, which will be displayed while the function is running, will be hidden if the function will work, or if a timeout exception has been selected.

Problem: I put code that performs all the functions described above in the onCreate () function. Nothing is displayed on the emulator screen, since the onCreate () function has not finished working ...

I also tried putting the code in the onStart () function ... the same undesirable results ...

I try to avoid the use of threads, because a function requires many variables that the thread will not have access to ...

How can I implement the desired behavior?

Thank.

+3
source share
2 answers
  • Create a class that implements Runnableand host all your loading logic. Call the function in action when done (say onFinished(params...))
  • Create a user interface Handlerand get a handler to update the interface inonFinished(params...)
  • Create a thread in onCreateand run it there to invoke Runnable.
+3
source

Source: https://habr.com/ru/post/1784522/


All Articles