title: How to pass data from a non-activity class to an activity in android?
body: <p>I'm building my app and I'm using socket.io to communicate with a webserver. I have java files that take care of that which are not activities and run in the background. So to start them, it will be instantiated in an activity and the sockets will run. Now the webserver triggers messages which I handle in the callback and pass the data to an activity. Now I'm looking for the best way to pass data from a running non-activity class to an activity currently running.</p>
  
  <p>What I did before was using an intent created by the non-activity class which sent the data to the currently running activity. The actvity had to be changed to singleinstance or singletop to avoid the activity from keeping on restarting on itself. 
  The problem is that form my main actvity I can't use single top launchmode because I need the actvity to refresh on itself in some instances. </p>
  
  <p>So I can't get the sockets properly working on the main activity because I can only recieve data if I change my main activity to singletop which is not ideal for the app. I had first tried nameing a function in the acivity that would be called by the non-activity class but it seems that, that's illegal, so I'm pretty stuck here.</p>
  
  <pre><code>@Override
      public void on(String event, IOAcknowledge ack, Object... data) {
  
          System.out.println("Server may have triggered event '" + event + "'");
  
          if (event.equals("message")) {
          System.out.println(data[2]);
  
          Intent newIntent = new Intent(Chat.mContent, Chat.class);
          Bundle extras=new Bundle();
          extras.putString("name", (String)data[1]);
          extras.putString("MSG", (String)data[2]);
          newIntent.putExtras(extras);
  
  
          Chat.mContent.startActivity(newIntent);
  
          }
          if (event.equals("ready")) {
              System.out.println("Received the ready callback");
              try{
              Intent newIntent = new Intent(TabExercise.mContext, TabExercise.class);
              newIntent.putExtra("ready", event);
              TabExercise.mContext.startActivity(newIntent);
              }catch(Exception e){
                  System.out.println("Caught an error");
              }
  
  
          }
      }
  </code></pre>
  
  <p>This is the method in the non-activity class where the data is sent.</p>
  
  <pre><code>@Override
  protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
  Bundle extras=intent.getExtras();
  
  if (intent.hasExtra("ready")) {
      // Retrieve the message and call getMsg(String)
     connect.starter(SaveSharedPreference.getName(TabExercise.this), SaveSharedPreference.getUserId(TabExercise.this));
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is the where the intent is caught in the mainactivity. Not ideal. </p>