There is a handler.

h = new Handler(Looper.getMainLooper()) { public void handleMessage(android.os.Message msg){ switch(msg.what){ case 1: // что то выполняется break; case 2: // что то выполняется break; case 3: // что то выполняется break; } }} 

I send him messages like this:

 Thread t = new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 100; i++) { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } h.sendEmptyMessage(i); }} }); t.start(); 

in each case the condition is checked, and if it truly would like to be able to stop all subsequent actions. For example, in case 1 condition is true, respectively, case 2 and all subsequent ones will not be fulfilled.

Forgive me if I put it somehow incorrectly, I explain as I understand myself at this stage.

  • Do you want to stop the flow? - get the volatile variable flag, put it in the handler, check it in the stream. Or do not want to perform actions? - add the flag too, check it before the switch ... You can make both checks and use the same flag. - Yura Ivanov

1 answer 1

The answer is based on the answer @Yura Ivanov Added variable int stop_handler = 1; I check before the switch

 h = new Handler(Looper.getMainLooper()) { public void handleMessage(android.os.Message msg){ if(stop_handler != 0) { switch(msg.what){ case 1: // при положительном результате проверки // выполняется присваивание значения 0 переменной // и соответственно дальнейшие действия не выполняются stop_handler = 0; break; case 2: // при положительном результате проверки // выполняется присваивание значения 0 переменной // и соответственно дальнейшие действия не выполняются stop_handler = 0; break; case 3: // при положительном результате проверки // выполняется присваивание значения 0 переменной // и соответственно дальнейшие действия не выполняются stop_handler = 0; break; } }}};