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
New Developer
Posts: 86
Registered: ‎01-20-2012
My Device: Bold Touch
My Carrier: Vodafone
Accepted Solution

Updating the main Screen from a background thread

Hi guys im trying to update the main screen from a backgrounf thread. in this case im using a 2 static imges. once i click on a butotn on the popupScreen when the application loads it will update the image on the main screen.

I have written a small snippet for it and can some one please give me some help with this. because when i debugged and one of the developers helped me with this to find out that the screen im trying to update is not the active screen.  please have  alook at the snippet please.ive got 3 classes here

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class MyApp extends UiApplication
{


    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.
        MyApp theApp = new MyApp();       
        theApp.enterEventDispatcher();
    }
    
    /**
     * Creates a new MyApp object
     */
    public MyApp()
    {        
        // Push a screen onto the UI stack for rendering.
        pushScreen(new MyScreen());
        final Pinpopup pp = new Pinpopup();    

        
        UiApplication.getUiApplication().invokeLater(new Runnable()
        {
            public void run()
            {                
                UiApplication.getUiApplication().pushModalScreen(pp);                
            }
        });    
    }    
}
////////////////////////////////////////////////////////////////////////////////////////

public final class MyScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
    public Bitmap bmp;
    public BitmapField bmpField;
    public ButtonField btnDisplay;
    public ImageThread imgThread;
        
    public MyScreen()
    {        
        
        // Set the displayed title of the screen       
        setTitle("MyTitle");
        bmp = Bitmap.getBitmapResource("image.png");
        btnDisplay = new ButtonField("Display");
        bmpField = new BitmapField(bmp);       

        
        
        btnDisplay.setChangeListener(new FieldChangeListener() {
            
            public void fieldChanged(Field field, int context) {
                // TODO Auto-generated method stub
                
                if(field == btnDisplay)
                {
//                    bmp = Bitmap.getBitmapResource("image1.png");
//                    bmpField.setBitmap(bmp);
                    //signInButtonClicked("07760926037", "1234");
                    LaunchImageThread();
                    
                }                
            }
        });
        
        add(bmpField);
        add(btnDisplay);
        
    }   
    
    
    public void LaunchImageThread()
    {
        imgThread = new ImageThread(this);
        System.out.println("THIS Screen: " +this.getScreen()); //  I figured out that the screen im updating is not the active but its strange i dont create any other refrence as well
        new Thread(imgThread).start();
    }    
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// POPUPSCREEN CLASS
class Pinpopup extends PopupScreen //implements FieldChangeListener
{        
    private ButtonField btnOk;
    
    Pinpopup()
    {
        
        super(new HorizontalFieldManager());

        btnOk = new ButtonField("Sign In");

        btnOk.setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context)
            {
                // TODO Auto-generated method stub
                try
                {
                    if(field == btnOk)
                    {        
                        close();
                        MyScreen ms = new MyScreen();
                        ms.LaunchImageThread();    
                    }
                }catch (IllegalArgumentException e) {
                    // TODO: handle exception
                    System.out.println("Popup exception while signingIn " +e.getMessage());
                }
            }
        });
        
        Manager fieldManagerContext = new Manager(USE_ALL_WIDTH)
        {
            public void sublayout(int width,int height) {               

                int xpos = 10;
                int ypos = 40;

                Field field = getField(0);
                layoutChild(field, 280, 50);
                setPositionChild(field, xpos+165, ypos+100);

                setPosition(300, 300);
                setExtent(350, 225);
            }
        };
        
        fieldManagerContext.add(btnOk);
        add(fieldManagerContext);
    }
}

 

/////////////////////////////////////////////////////////////////////////////////////////////////////

 

public class ImageThread implements Runnable
{
    private MyScreen parent;
    
    public ImageThread(MyScreen myScreen )
    {
        parent = myScreen;
    }

    
    public void run()
    {                
        try
        {
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                
                public void run() {
                    // TODO Auto-generated method stub
                    System.out.println("Active Screen: " + UiApplication.getUiApplication().getActiveScreen());
                    System.out.println("Active Screen: " + parent.getScreen());
                    parent.bmp = Bitmap.getBitmapResource("image1.png");
                    parent.bmpField.setBitmap(parent.bmp);
                    
                }
            });                                
        }
        catch(Exception ex)
        {
            System.out.println("Exception in Thread: " + ex.getMessage());
        }
    }
}

 

 

cheers thanks

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Updating the main Screen from a background thread

I suspect this is your problem:

 

                       MyScreen ms = new MyScreen();
                        ms.LaunchImageThread();  

 

I suspect if you had done as I suggested on the other Thread and put a break point in the constructor of MyScreen, you would have foiund that this creates a second instance.

 

Hopefully you can take it from here.

New Developer
Posts: 86
Registered: ‎01-20-2012
My Device: Bold Touch
My Carrier: Vodafone

Re: Updating the main Screen from a background thread

HI peter,

 

What you siad was right and I did try and i couldnt figure it out. anywyas thanks alot for your help Smiley Happy really appreciate it

Cheers.