title: How pass data between two activities while background activity always running
body: <p>So as illustration below. i want my background activity (stream data from bluetooth) places in class A always running whenever i change my screen to class B. The UI only update where the screen active, but using the same background activity that always running.</p>
  
  <p><img src="https://i.stack.imgur.com/oZiCW.png" alt="Illustration"></p>
  
  <p>I'm aware that there's only one active activity allowed to run on Android. That's way i try to pass the data using shared preferences from textview UI update on class A, and try to get it in class B. But, when i change the screen to class B, the background activity stop working. So, my shared preferences that i set up earlier only passed the last data.</p>
  
  <p>here's the code :
  Obtain data, class A</p>
  
  <pre><code>h = new Handler() {
              public void handleMessage(android.os.Message msg) {
                  switch (msg.what) {
                  case RECIEVE_MESSAGE:                                                   // if receive massage
                      byte[] readBuf = (byte[]) msg.obj;
                      String strIncom = new String(readBuf, 0, msg.arg1);                 // create string from bytes array
                      sb.append(strIncom);                                                // append string
                      int endOfLineIndex = sb.indexOf("\r\n");                            // determine the end-of-line
                      if (endOfLineIndex &gt; 0) {                                           // if end-of-line,
                          String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                          sb.delete(0, sb.length());                                      // and clear
                          txtArduino.setText("Data from Arduino: " + sbprint);            // update TextView
  
                          SharedPreferences logPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
                          SharedPreferences.Editor editor = logPreferences.edit();
                          String textLog = txtArduino.getText().toString();
                          editor.putString("log", textLog);
                          editor.commit();
  
                      }
                      //Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");
                      break;
                  }
              };
          };
  </code></pre>
  
  <p>receive data class B</p>
  
  <pre><code>package com.oding.skripsibluetooth3;
  
  import android.app.Activity;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  import android.widget.TextView;
  
  
  public class Datalog extends Activity{
  
      TextView tvDatalog;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_datalog);
  
          tvDatalog = (TextView) findViewById(R.id.tvDatalog);
  
          SharedPreferences logPreferences = PreferenceManager.getDefaultSharedPreferences(this);
          String text = logPreferences.getString("log", "null");
          tvDatalog.setText(text+"\r\n");
      }
  
  
  
  }
  </code></pre>
  
  <p>I appreciate any ideas, 
  i found that there's someone already encounter the <a href="https://stackoverflow.com/a/6148112/1446163">similar situation</a>. And i tried to put static on variable sbprint just like the solution stated, and there's red warning say "Illegal modifier for the variable sbprint; only final is permitted" 
  which one is simpler way for specific problem i have?shared-preferences?static variable?or what? and how do i resolve this?
  thank you</p>