Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30
Accepted Solution

Alt entry point, maintain single app instance!

Hey all,

 

Unlike most who implement alternate entry points in their BB apps, I want to maintain a single instance in which both the UI and the background processing code will be contained. In other words, no separate instance for UI and background processing. My alternate entry point is made to run on startup, in order to keep the background process running, even after device reboot.

 

My attempts thus far have been unsuccesful. I end up with multiple instances after reboots, which I can see in the bb app switcher (long press menu key).

 

Here is my code. I hope yall can find something in here that I'm doing wrong.  Thanks!

 

public class HelloWorldDemo extends UiApplication implements SystemListener
{
	public static HTTPInterface _httpInterface = new HTTPInterface(); 
	private static HelloWorldScreen _mainScreen;
        private static HelloWorldDemo theApp;
    	
private HelloWorldDemo() {                
        pushScreen(new AppScreen(this));  
     
    }
	
	 
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */ 
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
    	String deviceVersionString = DeviceInfo.getSoftwareVersion();
    	String deviceVersionSubString = deviceVersionString.substring(0, 3);
    	BingModel.deviceVersion = Double.parseDouble(deviceVersionSubString);
    	
    	if (theApp == null)
        {
        	theApp = new HelloWorldDemo(); 
        }
    	if (args != null && args.length > 0 && "startupEntry".equals(args[0]))
        {
    		
		//checking to see of app was set to run in background, before reboot occured.
    		boolean isChecked = BingModel.retrieveState();      

if(isChecked == true) { // If system startup is still in progress when this // application is run. if (ApplicationManager.getApplicationManager().inStartup()) { theApp.addSystemListener(theApp); } else { //theApp = new HelloWorldDemo(); theApp.doStartupWorkLater(); } //theApp = new HelloWorldDemo(); theApp.enterEventDispatcher(); } else System.exit(0); } else { BingModel.isRunStartup = false; //if (theApp == null) //{ /// theApp = new HelloWorldDemo(); //} theApp.enterEventDispatcher(); } } private void doStartupWorkLater() { invokeLater(new Runnable() { public void run() { doStartupWork(); } }); } private void doStartupWork() { BingModel.isRunStartup = true; UiApplication.getUiApplication().requestBackground(); } public void powerUp() { removeSystemListener(this); doStartupWork(); }
Please use plain text.
Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: Alt entry point, maintain single app instance!

Since auto-start and user started apps run in their own user space, I think you would need to use the runtime store to achieve this. Just create your screen on autostart and push a reference to the the runtime store then when the user clicks starts the app bring that screen to foreground.
Please use plain text.
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30

Re: Alt entry point, maintain single app instance!

Sorry but what is this runtime store you speak of? Could you point me to an example or provide one please?

 

By "their own user space" you mean the two can't function as a single instance?

 

Thanks for the reply!

Please use plain text.
Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: Alt entry point, maintain single app instance!

This shows how to create a singleton using the Runtime Store which is how you need to approach your problem. By "own user space", I mean if you have an autostart app then he user can also click on the application launching it a second time (which is probably what you are experiencing). This is why you will use the Singleton method and the Runtime Store to make sure only one instance of your screen exists and the it is globaly available on startup.

Please use plain text.
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30

Re: Alt entry point, maintain single app instance!

Only confused about one thing. Or maybe more, but for now this.

 

What is calling on getInstance()? Where does it come in with relation to the main() function?

 

Thanks!

Please use plain text.
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30

Re: Alt entry point, maintain single app instance!

public class HelloWorldDemo extends UiApplication implements SystemListener
{
	public static HTTPInterface _httpInterface = new HTTPInterface(); 
	private static HelloWorldScreen _mainScreen;
    private static HelloWorldDemo theApp;  
    private static final long GUID = 0x61019ed44dee9d44L; 
    
	private HelloWorldDemo() {                
        pushScreen(new AppScreen(this));  
     
    }
	

public static HelloWorldDemo getInstance() {      
		if (theApp == null) {         
			theApp = (HelloWorldDemo)RuntimeStore.getRuntimeStore().get(GUID);      
			if (theApp == null) {         
				HelloWorldDemo singleton = new HelloWorldDemo();         
				RuntimeStore.getRuntimeStore().put(GUID, singleton);         
				theApp = singleton;         
				}      
			}      
		return theApp;   
	}

 public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
    	String deviceVersionString = DeviceInfo.getSoftwareVersion();
    	String deviceVersionSubString = deviceVersionString.substring(0, 3);
    	BingModel.deviceVersion = Double.parseDouble(deviceVersionSubString);
    	
//...........etc

 

Please use plain text.
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30

Re: Alt entry point, maintain single app instance!

[ Edited ]

http://supportforums.blackberry.com/t5/Java-Development/UiApplication-put-into-RuntimeStore-vs-make-...

 

K after reading this thread, I am going to follow peter's advice and steer away from not only alternate entry points, but runtime store as well.

 

I realized that I can set the main app to run on startup, and when it does startup it will check for a persistent setting, which determines whether it will continue to run in background or exit. Much more simplistic. Once I have the code set and working I will post it here.

Please use plain text.
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30

Re: Alt entry point, maintain single app instance!

 This code is working perfectly for me right now. No alternate entries, and no runtime store.

It will work for anyone else who needs their app to run on startup but only remain running in the background if a setting had been set in the app to do so before the reboot or powerdown occured.

This line is retrieving a persistent var/setting, which determines whether or not to keep the app running in background at startup or to close it.

boolean isChecked = BingModel.retrieveState();

 

I hope this helps some other people!

 

 


public class DailyDesktop extends UiApplication implements SystemListener { public static HTTPInterface _httpInterface = new HTTPInterface(); private static HelloWorldScreen _mainScreen; private static DailyDesktop theApp; private DailyDesktop() { pushScreen(new AppScreen(this)); } /** * Entry point for application * @param args Command line arguments (not used) */ public static void main(String[] args) { // Create a new instance of the application and make the currently // running thread the application's event dispatch thread. String deviceVersionString = DeviceInfo.getSoftwareVersion(); String deviceVersionSubString = deviceVersionString.substring(0, 3); BingModel.deviceVersion = Double.parseDouble(deviceVersionSubString); theApp = new DailyDesktop(); theApp.addSystemListener(theApp); theApp.enterEventDispatcher(); } private void doStartupWork() { UiApplication.getUiApplication().requestBackground(); } public void powerUp() { removeSystemListener(this); boolean isChecked = BingModel.retrieveState(); if(isChecked == true) { BingModel.isRunStartup = true; doStartupWork(); } else { System.exit(0); } }

 

Please use plain text.