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
Contributor
Posts: 41
Registered: ‎11-11-2010
My Device: Not Specified
Accepted Solution

Why can't I restart the application immediately?

After reading around, I get the impression that best practice is to schedule an app restart for up to a minute later as follows:

 

ApplicationDescriptor descr = ApplicationDescriptor.currentApplicationDescriptor();
ApplicationDescriptor newDesc = new ApplicationDescriptor( descr,
	"My App Name",
	new String[] { "myArgument" },
	descr.getPosition(),
	descr.getNameResourceBundle(),
	descr.getNameResourceId(),
	ApplicationDescriptor.FLAG_AUTO_RESTART | ApplicationDescriptor.FLAG_SYSTEM
	);
ApplicationManager appManager = ApplicationManager.getApplicationManager();
appManager.scheduleApplication(newDesc,System.currentTimeMillis() + 60001, true);
System.out.println("Application is exiting..." );
System.exit(0);
return;

 

However, an immediate restart seems to work just fine, as follows:

 

ApplicationDescriptor descr = ApplicationDescriptor.currentApplicationDescriptor();
ApplicationDescriptor newDesc = new ApplicationDescriptor( descr,
	"My App Name",
	new String[] { "myArgument" },
	descr.getPosition(),
	descr.getNameResourceBundle(),
	descr.getNameResourceId(),
	ApplicationDescriptor.FLAG_AUTO_RESTART | ApplicationDescriptor.FLAG_SYSTEM
	);
ApplicationManager appManager = ApplicationManager.getApplicationManager();
try 
{
	appManager.runApplication(newDesc);
	System.exit(0);
	return;
} 
catch(ApplicationManagerException e) 
{
	// Handle situation when application cannot run
	System.out.println("Couldn't restart app: " + e.getMessage());
}

 

Can anyone explain why I should be doing it the first way instead please? I really do not want a delay if I can help it.

 

From examination of my output logs, I am also wondering why the scheduled app (with 60001 ms delay) often seems to run within 30 seconds of the restart. I am running on a 9930 simulator at present.

 

Thanks very much.

Developer
Posts: 455
Registered: ‎03-04-2011
My Device: Paper cup

Re: Why can't I restart the application immediately?

[ Edited ]

Think that if you just did amgr.launchApplication, if that somehow executed before the app had fully terminated, it might just switch the app back to the foreground, and then it finishes terminating.  So it would not launch.

 

but you could try it

 

An alternative would be to include an alternate entry point for the same app module, which directly in main simply does appmgr.launchApplication of what you're trying to restart.  Then you put a launchApplication for that alternate entry point where you currently have the scheduleApplication, and it would act like a relay, probably ensuring enough delay that the app will restart immediately after it finished terminating.

 

i.e.:

project 1:  CoffeeMaker.jdp
regular app, no arguments
module name: company_CoffeeMaker

project 2: CoffeeMakerReset.jdp
alternate entry point to CoffeeMaker
module name: company_CoffeeMaker
argument: Starter
check "System App"


class CoffeeMaker {
public static CoffeeMaker app;

public static void main(String args[]) {
if ((args.length > 0) && args[0].equals("Starter")) {
ApplicationManager.getApplicationManager().launchApplication("company_CoffeeMaker");
}
else {
app = new CoffeeMaker();
try {
app.enterEventDispatcher();
} catch (Exception e) {
ApplicationManager.getApplicationManager().launchApplication("company_CoffeeMaker?Starter");
}
}
}

}

 

Personally, I use scheduleApplication with a delay of 10 seconds.

 

Contributor
Posts: 41
Registered: ‎11-11-2010
My Device: Not Specified

Re: Why can't I restart the application immediately?

Hi silver20, thanks for your reply.

 

From your example, I'm not sure if I would need to set up two distinct projects, or simply use an alternate entry point in the same project. In my project, I am using a single project containing two AEPs. The UI AEP creates a UiApplication class and shows in the home screen and app switching panel. The background "monitor" AEP creates an Application class and is marked as "system" to hide it from the user.

 

So the fact that I am effectively creating a new instance of the app, surely means I can launch immediately? I think this is what you mean but would you mind confirming this please?

 

Many thanks.

 

(...further info/clarification: my project actually contains 3 entry points:

- starts when the user taps the icon from the home screen

- a starter "system" AEP which auto-starts and immediately launches the final one below. It then terminates itself.

- the main monitor AEP which is started by the above. This is made to auto-restart and is also a system process.)