title: Android: How do you pass a callback to an activity?
body: <p>I am creating a library. It creates an activity, starts it, and finishes it after a button is pressed.</p>
  
  <p>After the button is pressed, I'd like to execute some client code through an interface.</p>
  
  <p>That is, users of this library pass in an implementation, and I simply execute it.</p>
  
  <p>The analogous iOS code would be:</p>
  
  <pre><code>MyViewController *vc = [[MyViewController alloc] init];
  vc.callOnComplete = ...
  [self presentViewController:vc animated:YES completion:nil];
  </code></pre>
  
  <p>The desired code is:</p>
  
  <pre><code>public interface SaveStuff {
      void save();
  }
  </code></pre>
  
  <p>In my library's Activity:</p>
  
  <pre><code>private SaveStuff saveStuff;
  public void onButtonClicked(View v) {
      saveStuff.save() // client passes in saveStuff obj
      finish();
  }
  </code></pre>
  
  <p>I've tried serialization. I made a class that implemented the interface and also implemented <code>Serializable</code>, and tried passing that in and extracting it with <code>getSerializableExtra</code>. I end up getting a non serializable exception. So far as I know, this is probably not a viable solution, as it would mean that clients must use only serializable classes in their implementations. Correct me if I am wrong on this. In my trials, the client activity was showing up in the non serializable exception.</p>
  
  <p>I've tried using a static reference. Basically global state/singleton. As it turns out, according to a variety of answers such as this one: <a href="https://stackoverflow.com/questions/16736187/java-static-variable-becomes-null">Java Static Variable becomes null</a>, I cannot rely on any static state, and any solutions that do work with static state are just working coincidently.</p>
  
  <p>I haven't tried simply starting the activity with <code>startActivityForResult</code>, because that would require me to cast the context we are accepting as a parameter to an Activity. 
  The activity is started <strong>inside</strong> the library with this:</p>
  
  <pre><code>private void startNewActivity(int newVersion) {
      Intent i = new Intent(context, MyActivity.class);
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
      context.startActivity(i); // context variable is passed in by the client
  }
  </code></pre>
  
  <p>As you can see, context is passed in by the client. I could perhaps cast the context to an Activity in order to call <code>startActivityForResult</code>, but I don't want to do this, as I have read that is not always safe. I could require that an <code>Activity</code> be passed in. This is perhaps a reasonable requirement, but I don't want to go there just yet.</p>
  
  <p>I've looked at the existing questions and something I haven't tried is using broadcasts.  Correct me if I am thinking about this incorrectly, but the best I could do is have the object that holds the reference to client interface implementation be a broadcast receiver, and then send a broadcast in the activity that I create. I am worried that the receiver will not persist, however. What happens if my library shows the user the activity, but then the user kills the app before pressing the button? The reference to the client code was held by the previous activity. Will nothing happen upon pressing the button?</p>
  
  <p>Should I just broadcast a message, and leave it up to the client to handle these cases? How would the client handle this? Register something on app start up/shutdown? Do I have to force the user to do this to use my library? Is it reasonable?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>As it turns out, we are just going to have the activity in the library access a singleton.
  To avoid all the issues with state being wiped based on activities being killed and the like, users of this library will be required to run a 'setup' method on the singleton, and pass in the callback obj there. They will run this on some sort of base Activity's onResume.
  I'll wait a little before answering self-answering this question to see if there any alternatives. </p>