There is a class from which other classes will be inherited:

public class ActivityUART extends Activity { protected Handler handlerUART; private byte[] cmdBuffer; private class ReadThread extends Thread { @Override public void run() { super.run(); cmdBuffer = new byte[13]; while(!isInterrupted()) { // cmdBuffer[cntCmdByte] - заполняем буфер Message msg = null; msg = handlerUART.obtainMessage(cmdBuffer.length, cmdBuffer); handlerUART.sendMessage(msg); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected void onResume() { super.onResume(); mReadThread = new ReadThread(); mReadThread.start(); } @Override protected void onPause() { if (mReadThread != null) mReadThread.interrupt(); super.onPause(); } 

First grade:

 public class MainActivity extends ActivityUART { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onResume() { handlerUART = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); byte[] buffer = new byte[msg.what]; buffer = (byte[]) msg.obj; if (buffer[4] == 5) { startActivity(new Intent(MainActivity.this, timeActivity.class)); } }; super.onResume(); @Override protected void onPause() { super.onPause(); } 

Second class:

 public class timeActivity extends ActivityUART { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.newactivity); } @Override protected void onResume() { handlerUART = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); byte[] buffer = new byte[msg.what]; buffer = (byte[]) msg.obj; if (buffer[4] == 5) { Log.d(TAG, "execute key OK"); } }; super.onResume(); } @Override protected void onPause() { super.onPause(); } 

And now the question:

When you first transfer data from ReadThread to MainActivity everything happens, as it should be, timeActivity starts. But the next data transfer does not occur from ReadThread to timeActivity Log.d(TAG, "execute key OK"); , and instead, timeActivity starts timeActivity . If you transfer data from ReadThread to timeActivity , the Log.d(TAG, "execute key OK"); line Log.d(TAG, "execute key OK"); will be executed Log.d(TAG, "execute key OK"); . But if the hardware button of the android "back" to close the created timeActivity , return to the MainActivity and transfer the data again from ReadThread to the MainActivity , the Log.d(TAG, "execute key OK"); line Log.d(TAG, "execute key OK"); will still be executed Log.d(TAG, "execute key OK"); instead of the string startActivity(new Intent(MainActivity.this, timeActivity.class)); .

Why it happens? And how will I turn to the message handler of the current Activity?

  • Everything, figured out in the parent class ActivityUART handlerUART handler must be described as static protected, and everything will work. - selya
  • I almost lost my mind :) - selya
  • @selya, you would put your decision in the form of an answer and accept it. So, in the future, if someone comes across this, it will help him solve the problem. - Yuriy SPb
  • @ YuriySPB I did not have enough reputation points to answer my question. That's why I wrote in the comments - selya

1 answer 1

If the handlerUART handler in the parent class is described as protected Handler handlerUART; , when sending a message from a stream, the current Activity will not always be processed, although the handler itself is created in this Activity handlerUART = new Handler() { . If I understood correctly, if I describe the handler as described above, then the link to this handler will depend on the activities that are still in memory (I even had such that the message from the stream was processed by public void handleMessage(Message msg) from another program). In order to get a direct link to the handler, it must be described in the parent class as a static protected Handler handlerUART; . And then everything works as it should. Maybe this problem has more solutions, but I did not find them. If anyone knows about them, please write a big request !!!