Android Run A Function In a Class In New Thread
android, java, multithreading
Solution
I would suggest you use an `AsyncTask`. Its purpose matches exactly what you are trying to do. It allows you to run a background operation, and then update the UI with the result from such operation.
Problem
I have this code: ``` MyClass tmp = new MyClass(); tmp.setParam1(1); tmp.SetParam2("Test"); tmp.setParam3("Test"); ... ``` Then I have ``` tmp.heavyCalc(); ``` During this heavy calc operation I have to update progress bar in UI and show user that it's working with update in progress bar and some text to display. Now it doesn't work, because I'm not using thread, app becomes stuck and hanged, then suddenly returns that progressbar is 100% and all text all together appears suddenly. So I decided to make my function to run as new Thread. Inside definition of my class, I added `implements Runnabl`e So ``` public class MyClass implements Runnable{ ``` Then I put that `heavyCalc()` function to be called new `Run()` function I created: ``` @Override public void Run() { heavyCalc(); } ``` Now I do this: ``` Thread thread = new Thread(tmp); tmp.run(); ``` It works, but still no change at all in UI, app becomes stuck, then suddenly progressbar 100% and app returns. What I'm missing?