Problems withdraw the application for RuntimeStore

I have an application that listens to gps, e-mail and sms events. Because I need this application to run at startup of the bb, I used the method mentioned in many articles in kb by placing my application in the Bank to run at startup so that when my gui starts the user interface checks the runtime of the application store and proceeds accordingly. It all works perfectly. My problem comes when I need to stop the background of the GUI thread. I call shutdown in which the application stops all threads, that it is running and then I call a system.exit (0). All threads stop perfectly and closes my gui. However when I return to run the GUI with to restart the phone the waitForSingleton recover again the same background as application before having no threads running in it, and so nothing happens in the GUI (map changes). I also tried to remove the substantive application of the RuntimeStrore through .abolish. When I do cela background threads perfectly but the user interface may not start because an Exception is thrown stating that

application running in this process... However, this is not when I call the store of runtime. Any ideas as to what I'm doing wrong?

Thanks in advance

if(args.length>0 && args[0].equals("gui"))
        {

            System.out.println("Starting GUI");
            Controller controller=waitForSingleton();
            try{
            WhereAreYouMain app=new WhereAreYouMain(controller);
            app.enterEventDispatcher();
            }
            catch(Exception e)
            {
                System.out.println("************EXCEPTION: "+e.getMessage());
                return;
            }
        }
        else
        {

                boolean AutoStart=true;
                if(ApplicationManager.getApplicationManager().inStartup() && AutoStart)
                {
                    System.out.println("********************STARTING WHEREAREYOU*************************");
                Controller app=waitForSingleton();
                System.out.println("****************************Started WhereAreYOU**********************");
                }
            }
        }

    public static Controller waitForSingleton()
    {
        RuntimeStore store=null;
        store=RuntimeStore.getRuntimeStore();
        Controller controller=null;
        controller=(Controller)store.get(AppId);
        if(controller==null)
        {
            try{

            store.put(AppId, new Controller());

            }
            catch(Exception e)
            {
                System.out.println("Error putting app in storage"+e.getMessage());
            }
        }
        return (Controller)store.get(AppId);
    }
    public static void removeFromRuntimeStore()
    {
        RuntimeStore store=RuntimeStore.getRuntimeStore();
        store.remove(AppId);

    }
    public WhereAreYouMain(Controller controller)
    {
        WhereAreYouMainScreen screen=new WhereAreYouMainScreen(controller);
        controller.setMainScreen(screen);
        pushScreen(screen);
    }

When stop you your application, you also have to remove all listeners?  If one of them has a reference, your application will be not eligible for garbage collection and it will remain.

I understand that this approach (to put the application in RuntimeStore) is actually used in some articles in the KB, but I wouldn't recommend it.

Instead, I would say put you a small 'data only' RuntimeStore object, call this myRuntimeStore object.  I - this so that you can set a reference to your UiApplication in this document, for example. Have a myRuntimeStore.setMyApp (.) method.  Now your audience can get your app if they want, using myRuntimeStore.getMyApp ().  More importantly, you can erase it and make sure that your application will.  You may encode your listeners to do nothing if your application is not present.

However, I would like to recommend more than you do not actually store references to your application.  If you need something to initiate treatment in your application process, use a global event.  This beautifully isolates your application of your listeners.

Tags: BlackBerry Developers

Similar Questions

Maybe you are looking for