title: Return Callback to User from SDK In Android
body: <p>I have the following usecase :</p>
  
  <p>I have an initialization method, that the user calls. It has a callback listener attached to it, which should be called from the SDK on completion of the method. But, as major part of the code in the function is carried out on different threads and there are C/C++ methods invoked from the function, the last print statement in the initialization function, is carried out before the function actually finishes and I get the value back from the C/C++ function that is invoked.
  Right now, I am able to get the callback back from C/C++ code to the java layer signifying the end of initialization. But how do I further pass it as a callback to the user on completion of the initialization function in java(which the user calls).</p>
  
  <p>Here is my code :</p>
  
  <p>User code to call initialize method :</p>
  
  <pre><code>initialize(FiosTVApplication.getAppContext(), "CIAAACAKigE", new PostInitialization() {
          @Override
          public void complete() {
  
              MsvLog.d("&amp;&amp;&amp;&amp;", "At the end of onComplete Callback");
          }
      });
  </code></pre>
  
  <p>Interface for Callback :</p>
  
  <pre><code>public interface PostInitialization {
  
      void complete();
  </code></pre>
  
  <p>}</p>
  
  <p><strong>SDK code:</strong></p>
  
  <p>initialize method in java:</p>
  
  <pre><code>    public static void initialize(Context ctx, final String snippetId, PostInitialization listener)
  
      //c++ method calls and network calls
      listener.complete();
      log.d("SDK initialization complete"); // this is wrong as the c/c++ code execution and network calls is still not finished
  } 
  </code></pre>
  
  <p>End of initialization method which is called from c/c++ to let the java code know that initialization is complete. So, I want to call listener.complete().</p>
  
  <pre><code>public static int setEndOfInitialization(final boolean isEnd) {
  
      _mHandler = new Handler(Looper.getMainLooper());
      _mHandler.post(new Runnable() {
          @Override
          public void run() {
  
              listener.complete(); //this is not correct, for illustration only
              Logger.d("&amp;&amp;&amp;&amp;", " At the end of setEndOfInitialization" + isEnd);
          }
      });
  
      return 1;
  }
  </code></pre>
  
  <p>So, how do I pass a callback to users from initialize method(which the users will call) based on when setEndOfInitialization method is invoked, as when this method is called from c/c++ the initialization is truly completed.
  Both the initialize and setEndOfInitialization methods are in the same java file called Session.java.</p>