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: 32
Registered: ‎02-22-2011
My Device: Not Specified
Accepted Solution

Question regarding UiApplication.getUiapplicatin().

Hi to all,

                              Hope you all will be fine. Actually i want to ask that UiApplication.getUiApplication() gives you instance of your app.(please correct me if i am wrong). Consider the code 

public class SimpleSmsChatApp extends UiApplication  {
    
    // Constructor
    public SimpleSmsChatApp() {   
    
        //Create the main screen and push it on to the display stack
        SimpleSmsChatMainScreen mainScreen = new SimpleSmsChatMainScreen(chatScreen,  createProfileScreen);
        pushScreen(mainScreen);  
       
    } //end of constructor
    
    public static void main(String[] args) {
        
        //Create an instance of the application
        SimpleSmsChatApp app = new SimpleSmsChatApp();
        
        // Start the event displatch thread
        app.enterEventDispatcher();
        
    } // end of main()
    
   
} //end of class SimpleSmsChatApp

 It's a simple main class code. Now suppose i add a method in this class like this

public class SimpleSmsChatApp extends UiApplication  {
    
    
    private CreateProfileScreen createProfileScreen;
    private ChatScreen chatScreen;
    
    // Constructor
    public SimpleSmsChatApp() {   
    
        //Create the main screen and push it on to the display stack
        SimpleSmsChatMainScreen mainScreen = new SimpleSmsChatMainScreen(chatScreen, createProfileScreen);
        pushScreen(mainScreen);  
       
    } //end of constructor

        public static void main(String[] args) {
        
        //Create an instance of the application
        SimpleSmsChatApp app = new SimpleSmsChatApp();
        
        // Start the event displatch thread
        app.enterEventDispatcher();
        
    } // end of main()
    
    public ChatScreen getChatScreen() {

        if (chatScreen == null) {
  
            chatScreen = new ChatScreen();
             
        }

        return chatScreen;

    } //end of getChatScreen()

      public CreateProfileScreen getCreateProfileScreen() {

        if (createProfileScreen == null) {
  
            createProfileScreen = new CreateProfileScreen();
             
        }

        return createProfileScreen;

    } //end of getChatScreen()
     
} //end of class SimpleSmsChatApp

 Now i have another class in which i have buttons. Suppose on button clicks i want that createProfileScreen and chatScreen shows. I code it like this

 

 if (field == searchButton) {
                
     CreateProfileScreen createProfileScreen = UiApplication.getUiApplication().getCreateProfileScreen();                                                                                             UiApplication.getUiApplication().pushScreen(createProfileScreen);
             
 }
        
 if (field == chatButton) {
    
     ChatScreen chatScreen = UiApplication.getUiApplication().getChatScreen(); 

     UiApplication.getUiApplication().pushScreen(chatScreen);
          
 }

 My question is that

UiApplication.getUiApplication()

gives you the  instance of very same instance that i have created in my main class i.e. SimpleSmsChatApp. But this gives me error that can not find symbol getChatScreen().

I want  to ask that when

UiApplication.getUiApplication()

gives me the instance of main class (SimpleSmsChatApp) and i have getChatScreen() and getCreateProfileScreen() in my main class then why it gives me error that can not find symbol.

 

Actually i am doing all of this because i want that whatever number of screens i have in my project,  i created each screen instance only once and then call the instance whenever i need it  fron any class.

 

Please clear my confusion about UiApplication.getUiApplication(). Waht i am doing wrong and how can i achieve waht i am trying to do means created instance only once and call it when ever i need it from any class

 

Thanks

Developer
Posts: 141
Registered: ‎02-10-2010
My Device: 9500
My Carrier: Airtel

Re: Question regarding UiApplication.getUiapplicatin().

UiApplication.getUiApplication retrieves the current UiApplication instance.

 

Please change your code to typecast and then call the function like this:

((SimpleSmsChatApp)UiApplication.getUiApplication()).getChatScreen();

 

Contributor
Posts: 32
Registered: ‎02-22-2011
My Device: Not Specified

Re: Question regarding UiApplication.getUiapplicatin().

Thanks Pooja,

                                            It means UiApplication.getUiApplication() act like a singleton class?

 

I was wrong in casting...... Anyways thnaksSmiley Happy

Developer
Posts: 141
Registered: ‎02-10-2010
My Device: 9500
My Carrier: Airtel

Re: Question regarding UiApplication.getUiapplicatin().

Not a singleton class. In fact it returns the current object of the UiApplication that is already running and accessing itself from within the application.

Please mark the thread as resolved and press Kudos if it helped you.