title: How to pass data from BroadcastReceiver to Activity without in onCreate()
body: <p>I have a serious issue about passing data from <code>BroadcastReceiver</code> to an <code>Activity</code>. Let see my issue carefully. I have a class <code>PhoneStateReceiver extends BroadcastReceiver</code> that used to received an incoming phone. </p>
  
  <pre><code>public class PhoneStateReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          try {
              System.out.println("Receiver start");
              String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
              String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
              if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
  
              }            
          }
          catch (Exception e){
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>The incoming phone will be sent to an <code>Activity</code>, called <code>ReceiverActivity</code>. The <code>ReceiverActivity</code> receives the incoming phone and sends it to a server via a socket connection. The socket connection is initialized in the <code>onCreate</code> function. I googled and found server way to pass the data from <code>BroadcastReceiver</code> to an <code>Activity</code>. The common way is that send data via <code>putExtra</code> function and call <code>startActivity</code>. However, the way will call the <code>onCreate</code> again and then connect the socket, draw the UI again. Thus, it is not helpful in my case. </p>
  
  <p>In my goal, If the phone receives an incoming call, it will send the incoming call to the <code>ReceiverActivity</code>. The <code>ReceiverActivity</code> receives the message and calls the send function. Which is the best way to do it? Thank you</p>
  
  <p>The common way to pass data from a <code>BroadcastReceiver</code> to a <code>ReceiverActivity</code> that I used as follows</p>
  
  <p>In <strong>PhoneStateReceiver</strong> class :</p>
  
  <pre><code>Intent intent_phonenum = new Intent(context, ReceiverActivity.class);
  intent_phonenum.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  intent_phonenum.putExtra("phone_num", incomingNumber);
  context.startActivity(intent_phonenum);
  </code></pre>
  
  <p>In <strong>ReceiverActivity</strong> class :</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          connect_socket();
          Intent intent = getIntent();
          phone_num = intent.getStringExtra("phone_num");
          send(phone_num);
  }
  </code></pre>