To navigate through the pages of my app i use:
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
UiApplication.getUiApplication().pushScreen(screen);
}
});
This is working well on simple screens with not too many fields. But when I try to go to a Screen more elaborated (it contains a custom "listField" made out of multiple managers with multiple fields) the UI takes A LOT of time to update. I've noticed that the page has actually finished constructing but it just doesn't show up, I have to move the trackpad or do a click and then the screen will show.
Code for the screen:
public class List extends MainScreen{
public List(Categoria categoria){
Bitmap bmap = Bitmap.getBitmapResource("img/fondo.png");
getMainManager().setBackground(BackgroundFactory.createBitmapBackground(bmap, Background.POSITION_X_LEFT, Background.POSITION_Y_TOP, Background.REPEAT_SCALE_TO_FIT));
setTitle("Establecimientos");
vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
try {
DbHelper dbHelper = new DbHelper();
dbHelper.open();
EstablecimientoDAO eDao = new EstablecimientoDAO(dbHelper.db);
OfertaDAO oDao = new OfertaDAO(dbHelper.db);
Vector ests = eDao.getEstablecimientosPorCategoria(categoria);
for(int i = 0; i < ests.size(); i++){
Establecimiento est = (Establecimiento) ests.elementAt(i);
Oferta oferta = oDao.getOferta(est.getIdEstablecimiento());
EstablecimientosListField elf1 = new EstablecimientosListField();
Bitmap bm = null;
String location = "";
FileSystem fs = Utilities.getFileSystem();
if(fs.hasSdCard()){
location = "/SDCard/Blackberry/pictures/";
}
else if(fs.hasMCard()){
location = "/store/";
}
FileConnection fc = (FileConnection)Connector.open("file://" + location + "e_" + est.getIdEstablecimiento() + ".jpg");
if(fc.exists() && fc.fileSize() > 0){
InputStream is = fc.openInputStream();
byte [] data = new byte[(int) fc.fileSize()];
data = IOUtilities.streamToBytes(is);
is.close();
fc.close();
//bm = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);
EncodedImage eImage = EncodedImage.createEncodedImage(data, 0, data.length);
eImage = Utilities.resizeToWidth(eImage, Display.getWidth() / 3, 1, true);
Bitmap tmp = eImage.getBitmap();
if(tmp.getHeight() > 150){
eImage = Utilities.resizeToHeight(eImage, 1, 150, true);
}
bm = eImage.getBitmap();
}
else{
EncodedImage eImage = EncodedImage.getEncodedImageResource("img/default_esta.png");
eImage = Utilities.resizeToHeight(eImage, 1, 150, true);
bm = eImage.getBitmap();
}
String desc = "";
if(oferta != null && oferta.getDescripcion() != null){
desc = oferta.getDescripcion();
}
if(est.getDescEstablecimiento() != null){
desc += est.getDescEstablecimiento();
}
ButtonField verMas = new ButtonField("VER MÁS"){
protected boolean touchEvent( TouchEvent message ) {
int x = message.getX( 1 );
int y = message.getY( 1 );
if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
// Outside the field
return false;
}
switch( message.getEvent() ) {
case TouchEvent.UNCLICK:
fieldChangeNotify(0);
return true;
}
return super.touchEvent( message );
}
protected boolean navigationClick(int status, int time) {
if (status != 0) { // you did not have this check
fieldChangeNotify(0);
}
return true;
}
protected boolean trackwheelClick( int status, int time )
{
if (status != 0) fieldChangeNotify(0);
return true;
}
};
verMas.setCookie(est);
verMas.setChangeListener(new FieldChangeListener(){
public void fieldChanged(Field field, int context) {
final Establecimiento est = (Establecimiento)field.getCookie();
//if(field instanceof )
/*UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
UiApplication.getUiApplication().pushScreen(new DetalleEstablecimientoScreen(est));
}
});*/
Runnable runnable = new Runnable(){
public void run(){
UiApplication.getUiApplication().pushScreen(new DetalleEstablecimientoScreen(est));
}
};
PleaseWaitPopupScreen.showScreenAndWait(runnable, "Cargando...");
}
});
EncodedImage eImage = EncodedImage.getEncodedImageResource("img/det_fb.png");
eImage = Utilities.resizeToWidth(eImage, (int) (Display.getWidth() / 16), (int) (Display.getWidth() / 16), true);
BitmapField bmFb = new BitmapField(eImage.getBitmap(), Field.FOCUSABLE){
protected boolean touchEvent( TouchEvent message ) {
int x = message.getX( 1 );
int y = message.getY( 1 );
if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
// Outside the field
return false;
}
switch( message.getEvent() ) {
case TouchEvent.UNCLICK:
fieldChangeNotify(0);
return true;
}
return super.touchEvent( message );
}
protected boolean navigationClick(int status, int time) {
if (status != 0) { // you did not have this check
fieldChangeNotify(0);
}
return true;
}
protected boolean trackwheelClick( int status, int time )
{
if (status != 0) fieldChangeNotify(0);
return true;
}
};
bmFb.setCookie(est);
bmFb.setChangeListener(new FieldChangeListener(){
public void fieldChanged(Field field, int context){
try{
DbConnection dbHelper = new DbConnection();
dbHelper.open();
OfertaDAO oDao = new OfertaDAO(dbHelper.db);
final Establecimiento est = (Establecimiento)field.getCookie();
final Oferta oferta = oDao.getOferta(est.getIdEstablecimiento());
dbHelper.close();
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
try{
String post = "Banco de Bogotá";
if(oferta.getDescripcion() != null){
post = oferta.getDescripcion();
}
//String [] opciones = {"Aceptar", "Cancelar"};
int respuesta = Dialog.ask(Dialog.D_OK_CANCEL, "Usted está a punto de compartir la siguiente oferta:\n" + post, Dialog.YES);
if(respuesta == Dialog.D_OK){
new FacebookPost(oferta, est);
}
}
catch(Exception ex){
Dialog.alert("ERROR: " + ex.toString());
}
}
});
} catch(Exception ex){
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
Dialog.alert("Error al compartir");
}
});
}
}
});
eImage = EncodedImage.getEncodedImageResource("img/det_tw.png");
eImage = Utilities.resizeToWidth(eImage, (int) (Display.getWidth() / 16), (int) (Display.getWidth() / 16), true);
BitmapField bmTw = new BitmapField(eImage.getBitmap(), Field.FOCUSABLE){
protected boolean touchEvent( TouchEvent message ) {
int x = message.getX( 1 );
int y = message.getY( 1 );
if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
// Outside the field
return false;
}
switch( message.getEvent() ) {
case TouchEvent.UNCLICK:
fieldChangeNotify(0);
return true;
}
return super.touchEvent( message );
}
protected boolean navigationClick(int status, int time) {
if (status != 0) { // you did not have this check
fieldChangeNotify(0);
}
return true;
}
protected boolean trackwheelClick( int status, int time )
{
if (status != 0) fieldChangeNotify(0);
return true;
}
};
bmTw.setCookie(est);
bmTw.setChangeListener(new FieldChangeListener(){
public void fieldChanged(Field field, int context){
try{
DbConnection dbHelper = new DbConnection();
dbHelper.open();
OfertaDAO oDao = new OfertaDAO(dbHelper.db);
final Establecimiento est = (Establecimiento)field.getCookie();
final Oferta oferta = oDao.getOferta(est.getIdEstablecimiento());
dbHelper.close();
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
String tuit = "tweeteando desde fidelity";
if(oferta.getDescripcion() != null){
tuit = oferta.getDescripcion();
}
//String [] opciones = {"Aceptar", "Cancelar"};
int respuesta = Dialog.ask(Dialog.D_OK_CANCEL, "Usted está a punto de compartir la siguiente oferta:\n" + tuit, Dialog.OK);
if(respuesta == Dialog.OK){
UiApplication.getUiApplication().pushScreen(new BrowserFieldScreen(tuit));
}
}
});
} catch(Exception ex){
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
Dialog.alert("Error al compartir");
}
});
}
}
});
BitmapField pin = new BitmapField(Bitmap.getBitmapResource("img/pin.png"), Field.FOCUSABLE){
protected boolean touchEvent( TouchEvent message ) {
int x = message.getX( 1 );
int y = message.getY( 1 );
if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
// Outside the field
return false;
}
switch( message.getEvent() ) {
case TouchEvent.UNCLICK:
fieldChangeNotify(0);
return true;
}
return super.touchEvent( message );
}
protected boolean navigationClick(int status, int time) {
if (status != 0) { // you did not have this check
fieldChangeNotify(0);
}
return true;
}
protected boolean trackwheelClick( int status, int time )
{
if (status != 0) fieldChangeNotify(0);
return true;
}
};
pin.setCookie(new Long(est.getIdEstablecimiento()));
//pin.setChangeListener(this);
pin.setChangeListener(new FieldChangeListener(){
public void fieldChanged(Field field, int context) {
final long idEstablecimiento = ((Long)field.getCookie()).longValue();
//if(field instanceof )
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
UiApplication.getUiApplication().pushScreen(new MapaScreen(idEstablecimiento, "establecimiento"));
}
});
}
});
elf1.add(new BitmapField(bm));
elf1.add(new LabelField(est.getNombre(), LabelField.ELLIPSIS));
elf1.add(new LabelField(desc, LabelField.ELLIPSIS));
elf1.add(verMas);
elf1.add(pin);
elf1.add(bmFb);
elf1.add(bmTw);
vfm.add(elf1);
}
} catch (Exception ex){
}
add(vfm);
//UiApplication.getUiApplication().requestForeground();
//invalidate();
}
}
All of the answers given are very useful to have a better app and I'll do them, but the actual "error" I've found after debugging the code couldn't be guessed by the code I had given. The error was in a LabelField I passed to the manager, inside the manager I was changing the font of the Labelfield and was causing this crazy error. To resolve it I simply changed the font in the constructor of the mainscreen rather than inside the manager.