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?