Back Button handling in blackberry - 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.

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());
}

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;
}

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;
}

Related

Invoking navigationClick from navigationClick 'father' in Blackberry

Edited
...
I´m trying to deploy a new component in my BlackBerry app. I have a new class that extends of HorizontalFieldManager, where I instanced a new ListField. Inside of this, I overwrote the navigationClick method and I open a popup. The name of this component is CustomComponentHorizontal. I use this component in other component customized, its name is CustomComponentVertical. This is the code:
In Main.java:
...
CustomComponentVertical ccv=new CustomComponentVertical();
...
add(ccv);
In CustomComponentVertical
...
CustomComponentHorizontal cch=new CustomComponentHorizontal{
...
protected boolean navigationClick(int status, int time) {
//Should I do something here??
return super.navigationClick(status, time);
}
};
...
add(cch);
In CustomComponentHorizontal:
public class CustomComponentHorizontal extends HorizontalFieldManager {
ListField choiceField=null;
PopupScreen popup=null;
public CustomComponentHorizontal (){
choiceField = new ListField(){
public boolean navigationClick(int status, int time) {
Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
if (focus instanceof ListField) {
popup = new ChoicePopupScreen(10, 50, choices);
popup.setChoice(choices);
UiApplication.getUiApplication().pushScreen(popup);
setPopup(popup);
return super.navigationClick(status, time);
}
};
}
}
}
My purpose is that when I click in my component, it is launched the navigationClick of listfield. When I put the focus in my component and click with trackpad, the popup no open. But, If I touch the screen over the component, and then I click with trackpad, popup open.
How could I open the popup from component without to use the touch event?
Thank you very much.
I copy pasted your code. There were some errors, and your callback was missing. So I fixed it up a bit and created a simple callback to test with, and the navigationClick fires. Hopefully this helps, let me know if there is a different issue I didn't understand.
public CustomComponent()
{
elements = new Vector();
elements.addElement("Element 0");
elements.addElement("Element 1");
elements.addElement("Element 2");
elements.addElement("Element 3");
elements.addElement("Element 4");
elements.addElement("Element 5");
choiceField = new ListField(elements.size())
{
public boolean navigationClick(int status, int time)
{
int index = getSelectedIndex();
Dialog.inform((String) elements.elementAt(index));
return true;
}
};
choiceField.setCallback(new ListFieldCallback()
{
public int indexOfList(ListField listField, String prefix, int start)
{
return elements.indexOf(prefix, start);
}
public int getPreferredWidth(ListField listField)
{
return Display.getWidth();
}
public Object get(ListField listField, int index)
{
return elements.elementAt(index);
}
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
String obj = (String) get(listField, index);
graphics.setColor(0x000000);
graphics.drawText(obj, 0, y, 0, width);
}
});
add(choiceField);
}

cannot close the screen in blackberry

I am using finish() to close current activity before quit application in Android.
However, I cannot close screen in blackberry.
public class Main_AllLatestNews extends MainScreen {
public Main_AllLatestNews() {
super(USE_ALL_WIDTH);
}
private boolean Dialog() {
final Bitmap logo = Bitmap.getBitmapResource("icon.png");
d = new Dialog("确定离开?", new String[] { "是", "否" }, new int[] {
Dialog.OK, Dialog.CANCEL }, Dialog.OK,
logo) {
public void setChangeListener(FieldChangeListener listener) {
if (d.getSelectedValue() == Dialog.OK) {
} else {
d.close();
}
};
};
d.show();
return (d.doModal() == Dialog.OK);
}
public boolean onClose(){
if(Dialog()){
System.exit(0);
return true;
}else
return false;
}
}
Here is my Main class
public class Main extends UiApplication {
public static void main(String[] args) {
Main theApp = new Main();
theApp.enterEventDispatcher();
}
public Main() {
pushScreen(new MyScreen());
}
public final class MyScreen extends MainScreen {
private Bitmap logo = Bitmap.getBitmapResource("logo_page.png");
private BitmapField bmfield;
public MyScreen() {
setTitle("Oriental Daily");
bmfield = new BitmapField(logo, Field.FIELD_HCENTER
| BitmapField.FOCUSABLE) {
protected boolean navigationClick(int status, int time) {
Main.this.pushScreen(new Main_AllLatestNews());
Main.this.popScreen(MyScreen.this);
return true;
}
};
}
}
It depends on exactly how you want your close behaviour to work. Also, I can only read English, so I'm not 100% sure what your Dialog says. I'm assuming it's something to do with closing the app (yes or no)?
Anyway, usually, my apps close by overriding the onClose() method in the MainScreen subclass. You don't actually need to listen for the escape key. onClose() will get called normally when the user escapes all the way out of the app, or presses the little button with the blackberry icon, and then selects Close.
public final class MyScreen extends MainScreen {
/** #return true if the user chooses to close the app */
private boolean showDialog() {
Bitmap logo = Bitmap.getBitmapResource("icon.png");
Dialog d = new Dialog("确定离开?",
new String[] { "是", "否" },
new int[] { Dialog.OK, Dialog.CANCEL },
Dialog.OK,
logo);
return (d.doModal() == Dialog.OK);
}
/** Shutdown the app? */
public boolean onClose() {
if (showDialog()) {
System.exit(0);
return true;
} else {
// the user does not want to exit yet
return false;
}
}
}

setChangeListener method is not invoked

i am new in blackberry developer. i am use a pillsetbutton and pillfieldbutton
but when i am click pillfieldbutton no any action is performed.I am using setchangeListener() method.but no any Action is performed.i am going Through this process.
public DemoPill() {
PillButtonSet objButtonSet=new PillButtonSet();
final PillButtonField objButtonField1=new PillButtonField("NSE");
final PillButtonField objButtonField2=new PillButtonField("BSE");
objButtonSet.add(objButtonField1);
objButtonSet.add(objButtonField2);
this.add(objButtonSet);
bjButtonSet.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Hi ");
if(field==objButtonField1)
{
System.out.println("This Is NSE Button");
}
else if(field==objButtonField2)
{
System.out.println("This Is BSE Button");
}
}
});
}
}
You are printing it on console. So without debugging the code you will never know if your click is consumed. So just use an event thread to see the output on your screen. I have provided you the sample just check it. It will show the output on your screen. You can also use Dialog.inform(String message ) But its always good to do it on event thread.
public DemoPill() {
PillButtonSet objButtonSet=new PillButtonSet();
final PillButtonField objButtonField1=new PillButtonField("NSE");
final PillButtonField objButtonField2=new PillButtonField("BSE");
objButtonSet.add(objButtonField1);
objButtonSet.add(objButtonField2);
this.add(objButtonSet);
bjButtonSet.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// System.out.println("Hi ");
if(field==objButtonField1)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("objButtonField1 button clicked")
}
});
}
else if(field==objButtonField2)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("objButtonField2 button clicked")
}
});
}
}
});
}
}
May be this will help cheers. :)
You only can view the output of
System.out.println("ANYDATA");
in debug mode not in run.
Try to debug it not to run it.

create custom menu on blackberry

I create a custom menu in new class extends popupScreen. I went when the user clock on the menu button the menu display. I use this code
protected boolean keyDown(int keycode, int time) {
if(Keypad.KEY_MENU == Keypad.key(keycode))
{
UiApplication.getUiApplication().popScreen(getScreen());
Menu popup = new Menu();
UiApplication.getUiApplication().pushScreen(popup);
return true;
}
else
return super.keyDown(keycode, time);
}
but I obtain error on this line UiApplication.getUiApplication().pushScreen(popup); causes by pushScreen. How can I change this code or there is another method to display this menu.
try this
protected boolean keyDown(int keycode, int time) {
if(Keypad.KEY_MENU == Keypad.key(keycode))
{
synchronized (UiApplication.getEventLock())
{
UiApplication.getUiApplication().popScreen(getScreen());
Menu popup = new Menu();
UiApplication.getUiApplication().pushScreen(popup);
}
return true;
}
else
return super.keyDown(keycode, time);
}
In Blackberry, creating, displaying and hiding of Menu is handled by the system; you will be provided with callbacks at appropriate time. The callbacks you will need to code are:
public boolean onMenu( int instance )
{
return true; // Menu is created
}
public void makeMenu( Menu menu, int instance )
{
MenuItem _desired_menu_item = new MenuItem()
{
public void run()
{
}
}
menu.add( _desired_menu_item );
super.makeMenu( menu, instance)
}
That's it! It would work. Please check Blackberry "UI and Navigation guide" for more details

CustomButtonField in BlackBerry

Plz brifly explain the meaning of ....
First i create a CustomButtonField class, and then i found out that if i din't write the
public boolean isFocusable()
{
return true;
}
function, it doesnot responds to user events like mouse click....
and also plz explain briefly the meaning of the following functions
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time)
{
if (character == Keypad.KEY_ENTER)
{
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
If you want a field to behave like a button
you must set Field.FOCUSABLE style bit.
setChangeListener to CustomButtonField.
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
if user clicks on this button, we can see that button has changed (fieldChangeNotify(0);)
and listener of the button works.
protected boolean keyChar(char character, int status, int time)
{
if (character == Keypad.KEY_ENTER)
{
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
by this method if 'Enter key' pressed, button says i am changed.(fieldChangeNotify(0);)

Resources