0

Even though the while loop is in a thread, The UI does not load till the while break for any condition. I want the UI to load inspite of the infinite while loop running. How to handle such situation ?

public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* runOnUiThread th=new runOnUiThread(new TextChange());*/
    new Thread() {
        public void run() {
            try {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // change UI elements here
                                TextView tv = (TextView) findViewById(R.id.tv);
                                while (true) {
                                    int y = 10;
                                }
                            }
                        });
                    }
                });
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
alexandresaiz
  • 2,678
  • 7
  • 29
  • 40
  • your while loop is infinite loop.Please work on that part – Amarjit Sep 21 '15 at 06:49
  • 1
    Because you are running your loop in the `runOnUiThread` which is responsible for UI events thats why it wont load the UI...The required Solution is to run your loop in thread using `new Thread`... – Burhanuddin Rashid Sep 21 '15 at 06:56
  • i even tried that , but it doesnt work – kishlay raj Sep 21 '15 at 07:01
  • i have edited the code with new Thread but still doesn't work @BurhanuddinRashid – kishlay raj Sep 21 '15 at 07:12
  • Because still your are running infinite loop in `runOnUiThread` thats why the UI stuck in the infinite loop...Don't Run `while(true)` in your `runOnUiThread` – Burhanuddin Rashid Sep 21 '15 at 07:19
  • but is there any way to load to UI while the Loop is running , bcz i hav written a similar problem which continuously take sound input and bcz of that the ui does not load http://stackoverflow.com/questions/32666208/why-the-ui-does-not-show-till-the-while-loop-breaks @BurhanuddinRashid – kishlay raj Sep 21 '15 at 09:05

3 Answers3

1

First of all the runOnUiThread doesn't have any effect as all of the code inside the onCreate method runs on the UI thread by design, that means you don't have put it inside a separate runOnUiThread block. Secondly, you'll have to use an AsyncTask to to create a new background thread as its more optimized to do small background stuff and also interact with the main UI thread. And third, you're creating an infinite loop which means it won't come out of it. Check that out.

zulkarnain shah
  • 971
  • 9
  • 20
  • but is there any way to load to UI while the Loop is running , bcz i hav written a similar problem which continuously take sound input and bcz of that the ui does not load http://stackoverflow.com/questions/32666208/why-the-ui-does-not-show-till-the-while-loop-breaks – kishlay raj Sep 21 '15 at 09:04
  • AsyncTask is your answer as I already mentioned. This class is designed specifically to handle things like you've mentioned. Extend a class from AsyncTask, and then do the UI loading stuff in its onProgressUpdate() method while doing the heavy non-UI functionality in its doInBackground() method – zulkarnain shah Sep 21 '15 at 09:22
0

try this code:

new Thread() {
    public void run() {

        try {                                  
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // change UI elements here
                            TextView tv = (TextView) findViewById(R.id.tv);
                            new Thread(){
                                public void run() {
                                    while (true) {
                                        int y = 10;
                                    }
                                }
                            }.start();
                        }
                    });               
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}.start();
nariman amani
  • 263
  • 6
  • 21
0

There's some issues with your code (as others mentioned) but the main problem here is a mis-understanding of what the UI thread is.

As described in "Rendering Performance 101" when your process is launched, Android will create a "main" thread for it. This thread will handle all your input callback events, as well as handle rendering operations for you. The rendering system itself will try to re-draw the screen every 16ms or so. If there's work on your main thread (also called your "UI thread") when one of these 16ms pluses happens, then you'll get what we call a "dropped frame" (see "Why 60fps?").

As such, most Android developers do everything they can to get work OFF the UIThread/MainThread so that there's no contention with the rendering system (and thus, no dropped frames!)

Your code is executing a "runOnUIThread" callback, which pushes a bundle of work to execute on the UI thread. Unless the work in that routine executes in <16ms there's a chance it will force your application to miss a frame; or worse, to not render the screen until the loop is finished.

What you really want to do instead is use something like AsyncTaskLoader to move your infinite loop OFF the UI thread.

Colt McAnlis
  • 3,846
  • 3
  • 18
  • 19