user1213202
user1213202

Reputation: 1305

Back Button handling in blackberry

I want to come to home screen when clicking on the back button from my application in blackberry. I have searched for this in google and stackoverflow, But I didn't get any solution. Anybody help for this.

In My Starting Screen i wrote like this for back button.

protected boolean keyDown(int keycode, int time)
    {
        if (Keypad.key(keycode) ==  Keypad.KEY_ESCAPE)
        {
            close();
            return true;
        }

        else
        {
            return super.keyDown(keycode, time);
        } 
    }

In my HomeScreen i wrote like this.

protected boolean keyDown(int keycode, int time)
    {
        if (Keypad.key(keycode) ==  Keypad.KEY_ESCAPE)
        {
            UiApplication.getUiApplication().pushScreen(new StartingScreen());
            return true;
        }

        else
        {
            return super.keyDown(keycode, time);
        } 
    }

From my application HomeScreen I am able to come to starting screen of the my application. after that when i click on back button from starting screen i need to go to blackberry home screen that means i need to exit the app and come out of that. I wrote close(); to come to blackberry home screen. but its not working. it is again coming to my application homescreen.

Upvotes: 2

Views: 4049

Answers (3)

user1213202
user1213202

Reputation: 1305

I got the Solution... i have written lik this.

public boolean onClose() 
    {
        int choose=Dialog.ask(Dialog.D_YES_NO, "Are you sure Want to Exit?");
        if(choose==Dialog.YES)
        {
            System.exit(0);

        }
        return true;
    }

Upvotes: 3

Nilanchala
Nilanchala

Reputation: 5941

Override keyDown method in your subclass of MainScreen.

protected boolean keyDown(int keycode, int time) {  
    int key = Keypad.key(keycode);
        if(key==Characters.ESCAPE){
            // do something here
            return true;
        }       
        return super.keyDown(keycode, time);
    }

To come to home screen

while(!(UiApplication.getUiApplication().getActiveScreen() instanceof HomeScreen)){
                UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}

Upvotes: 5

Rince Thomas
Rince Thomas

Reputation: 4158

Try this -

public class yourclass extends MainScreen{
      public yourclass(){

   }
   }
public boolean onClose() {
       Application.getApplication().invokeLater(new Runnable() {
            public void run() {

                             //close this screen and push your home screen  

            }
        });
        return true;
    }

Upvotes: 1

Related Questions