Make a running UI application go to the background and resume in the foreground

by Retired ‎02-16-2010 01:27 PM - edited ‎09-16-2010 03:50 PM (8,662 Views)

Summary

 

This article applies to the following:

  • BlackBerry® smartphones based on Java® technology
  • BlackBerry® Java® Development Environment (BlackBerry JDE)

Description

 

If an application is designed such that the icon is an alternate entry point, you must make the main()method avoid creating a new instance of an application that is already running.

 

The alternate entry is going to call the main method with the parameter that is passed in, regardless of whether the application is running.

 

Note: Remember that the alternate entry point is NOT the application. Think of it as a batch file that starts an alternate; the file is unaware that the alternate is running.

 

To get around this, register a singleton instance of the application with the system, then query the system on startup to see whether it is running.

 

Note: This is possible with BlackBerry JDE 3.6 and NOT with BlackBerry JDE 3.2 Service Pack 1.

 

Use the following code sample:

 

 

//register the alternate on first run
RuntimeStore appReg = RuntimeStore.getRuntimeStore();
synchronized(appReg){
   if (appReg.get(ID) == null) {
       appReg.put(ID, new Application(...));
   }
   Application MyApp = (Application)appReg.waitFor(ID);
   //then you have the app MyApp... return it or whatever
}

 

-THEN-

 

 

//check to see if the app exists, if it does, bring it to the foreground ...
RuntimeStore appReg = RuntimeStore.getRuntimeStore();
synchronized(appReg){
   if (appReg.get(ID) != null){
       Application MyApp = (Application)appReg.waitFor(ID);
       MyApp.requestForground();
   }
}