1

In my Android app, I have some code that is running in the background, in its own thread. This code contacts a server on the network to acquire data. Once that data is acquired, I need to be able to access my activity's view so that I can change data on the screen (images, text, etc.). Here is the way my packages are set up:

|- Java
|  `- myMainPackage
|     |- subPackage01
|     |  `- classIWantToAccessMyLayoutFrom.java // different thread
|     |- subPackage02
|     |- subPackage03
|     `- subPackage04
|        |- subSubPackageA
|        |  `- MainActivity.java
|        |- subSubPackageB
|        |- subSubPackageC
|        |  `- MyLayout.java
|        `- subSubPackageD
|
`- res
   |- drawable
   |- layout
   |  |- activity_main.xml
   |  `- my_layout.xml
   `- values

I just cannot figure out how to get access to the activity from within classIWantToAccessMyLayoutFrom.java. Do I pass the activity into classIWantToAccessMyLayoutFrom.java in the constructor when it is created, or is there a better, more "slick" way, to do it?

If I pass it to the thread's constructor, I will have to pass the activity through all of the other classes until it gets to the actual class that needs it. It just seems like there should be a better way. Is there?

Brian
  • 1,726
  • 2
  • 24
  • 62

2 Answers2

3
runOnUIThread( // set values or another runnable or whatnot here);

Please see a popular answer here:

how to use runOnUiThread

Example :

private void runThread() { 

    new Thread() {
        public void run() { 
            while (i++ < 1000) { 
                try { 
                    runOnUiThread(new Runnable() {

                        @Override 
                        public void run() { 
                            btn.setText("#" + i); 
                        } 
                    }); 
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } 
            } 
        } 
    }.start();
} 
Community
  • 1
  • 1
childofthehorn
  • 697
  • 1
  • 4
  • 12
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/10993664) – Gabriele Mariotti Jan 22 '16 at 17:12
  • example from link included – childofthehorn Jan 22 '16 at 18:48
  • @childofthehorn Even though I have imported android.app.Activity, I cannot get runOnUiThread to work. I keep getting "cannot resolve method" error. – Brian Jan 22 '16 at 21:16
  • @childofthehorn I figured it out. I passed my main activity into my thread in the constructor and used that to get access to runOnUiThread. – Brian Jan 22 '16 at 22:11
0

The preferred way of doing this is using a AsyncTask (assuming that the activity doesn't get destroyed within the download time) instead of creating a separate thread.

public class ClassIWantToAccessLayoutFrom extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {
        // Download content from URLs here.
    }

    protected void onProgressUpdate(Integer... progress) {
        // This is run on the UI thread.
        // Update any UI elements to report progress here.
    }

    protected void onPostExecute(Long result) {
        // Your async task has completed.
        // This runs on UI thread as well.
    }
}

You can pass your view or activity into this class and update it in onProgressUpdate or in onPostExecute.

For reference check http://developer.android.com/reference/android/os/AsyncTask.html

vivekprakash
  • 346
  • 2
  • 9
  • I tried to convert my thread to AsyncTask, but I keep getting errors. I have "foo extends AsyncTask" and "protected void doInBackground(View myView)" but Android Studio keeps telling me I need to make this class abstract or implement the doInBackground method, and I have already implemented this method. – Brian Jan 22 '16 at 20:47
  • AsyncTask is now deprecated: This class was deprecated in API level 30. Use the standard java.util.concurrent or Kotlin concurrency utilities instead. – user1053510 Apr 02 '21 at 12:34