Search code examples
androidbackground-process

Loading UI elements before the content loads Android


I am developing an app where it gathers pictures posted by the users in a GridView in an activity. But when I try to go the that activity from another one on let's say buttonClick, there is a long black screen and when the activity with the pictures come , it comes with the pictures loaded. How do I instantly go the that activity and then one by one load the pictures in the background ??


Solution

  • You should be doing anything resource heavy (loading pictures) on a seperate thread. The UI thread should be loading and controlling the UI only. Look into using AsyncTasks as they are the most used method of multithreading in android. Here's a quick example:

    private class DownloadFilesTask extends AsyncTask<URL, Integer, ArrayList<Image>> {
     protected Long doInBackground(URL... urls) { //runs on seperate thread
         //Load Pics...
         //Return Pics...
     }
    
     protected void onPostExecute(ArrayList<Image> result) { //runs on UI thread
         //Load Pics into UI...
     }
    

    }