Access views from another stream (Android)

I am developing an application for Android, and I have a button that starts / pauses a specific modeling process. While this process is running, I need to output some data from it in real time. But when I create a new stream for modeling, I can’t access the views (let it be a TextView) from this stream, because they can only be obtained from the stream in which they were created. On the other hand, a new thread is necessary, because otherwise the user will not be able to do anything while the simulation is running (for example, click several other buttons). Creating a new service also requires creating a new thread in this case. How do I solve this problem?

+6
source share
5 answers

You can handle it in many ways,

  • Try using AsyncTask in this, your background work done in doInBackGround () and your user interface will not be blocked, and you can also access views of the activity with which you call AsyncTask your context through publishProgress () and onProgressUpdate () .

  • If you use a simple Thread , and then using a Handler or a message or runOnUiThread , you can update the view of the main thread.

but in your way I think AsyncTask is best for you.

+5
source

You can solve the problem with android.os.Handler . The Handler instance is bound to the thread that creates it, you can send Runnables to it, and it will execute them in the thread to which it is bound. For instance:

 import android.os.Handler; import android.widget.TextView; public class MyActivity extends Activity { private Handler uiHandler; private TextView simulationStatus; @Override public void onCreate(Bundle savedInstanceState) { ... uiHandler = new Handler(); ... simulationStatus = (TextView) findViewById(R.id.simulationStatus); ... } // This method is to be executed on the simulation thread. public void simulate() { ... final String simulationStatusText = ...; ... uiHandler.post(new Runnable() { @Override public void run() { // This is run on the UI thread. simulationStatus.setText(simulationStatusText); ... } }); } ... } 

You can also use AsyncTask . See Link, for example.

+2
source

Access to them is possible, but read-only. You mean that you want to initiate changes from the stream in the views. From a workflow (non-UI) these changes cannot be activated, you need to use .runOnUiThread() or use Handlers . Use them in the place where you want to show something, for example, update the text in .runOnUiThread (),

Example:

 myThread = new Thread() { @Override public void run() { //yourOperation MyActivity.this.runOnUiThread(new Runnable(){ @Override public void run() { if(e!=null) { myTextView.setText("something"); } }}); super.run(); } }; myThread.start(); 
+2
source

Pass the activity into your stream and use it from the inside to access your views.

 // from your activity: myExternalThread = new MyCustomCode(this); // <-- activity passed // your "simulation process" constructor: public MyCustomCode(Activity parent) { this.parent = parent; } // and when you want to access views: parent.runOnUiThread(new Runnable() { @Override public void run() { parent.findViewById(R.id.text_view); ... do something to your UI ... } }); 

My preferred way to create these external tasks is to use AsyncTask (then you don't have to worry about runOnUiThread , among other benefits). I do not know if this is relevant to your case, but you should check it.

+1
source

you need to save the data that you want to show in the public var and let your thread call Runnable through Handler.post (runnable) The code in the run () method of Runnable has access to Textview

+1
source

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


All Articles