java - How do I init my object in Application instance asynchronously? -


i have messaging "service"(not android service) , want initialize once in applicaion's oncreate , provide "service" app componens.

the problem messagebus should initialized on separate thread, since performes network operations.

how eliminate race condition if client tries access messagebus instance? say, activity tries messagebus, it's still not initialized?

   public interface serviceprovider {         public messagebus getmessagebus();     }       public class myapplication extends application implements serviceprovider {         private messagebus messagebus;          @override         public void oncreate(){             super.oncreate();              new thread(new runnable() {                  @override                 public void run() {                     try {                         messagebus = new messagebusimpl();                     } catch (exception e) {                         log.d(tag, "failed init messagebus");                         e.printstacktrace();                     }                 }             }).start();         }      override     public messagebus getmessagebus() {         return messagebus;     } } 

you can add splash screen activity app (or show progress bar in dialog or lock app other way want) , keep visible long app elements initialized. showing user (especially if init operation can take while) quite important gives sort of feedback user app in fact doing something.

edit

if want notify other components setup done, observer pattern (wiki) should serve purpose. i.e in application object expose methods setonsetupcompletedlistener() , when setup done, let registered listeners know.


Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -