Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run thread from another thread

I test the following code and the Toast message did not appear and the "TestMethod" did not call "Catch" method , please help me ?

public void TestMethod()
 {
     Test= new Thread(new Runnable() {
         public void run() {
             try{
                Catch(); 
             }
             catch (Exception ioe) 
             {

             }

         }
     });
     Test.start();
 }
public void Catch()
 {
     Test2= new Thread(new Runnable() {
         public void run() {
             try{
                 Toast.makeText(getApplicationContext(), "Yes", Toast.LENGTH_SHORT).show();
             }
             catch (Exception ioe) 
             {

             }

         }
     });
     Test2.start();
 }
like image 823
AndroidDev Avatar asked Dec 01 '22 07:12

AndroidDev


2 Answers

May be runOnUiThread helpful to you.

  • runOnUiThread lets you ride on the UI thread and let s you to perform action on UI thread.

Try this:

runOnUiThread(new Runnable() 
{
      public void run() 
      { 
         Toast.makeText(getApplicationContext(), "Yes", Toast.LENGTH_SHORT).show(); 
      }
});
like image 81
Paresh Mayani Avatar answered Dec 11 '22 08:12

Paresh Mayani


You should call Toast.makeText on UI thread. Read this for more details.

like image 26
Mikita Belahlazau Avatar answered Dec 11 '22 07:12

Mikita Belahlazau