1

I just tried to implement a back functionality in my app. I tried the example in [CodeNameOne Dynamically created Form, how to "Back":

Command back = new Command("Back") {
     public void actionPerformed(ActionEvent ev) {
         // notice that when showing a previous form it is best to use showBack() so the 
         // transition runs in reverse
         showPreviousForm();
     }
};
f.setBackCommand(back);

I added some log messages to see whether the button is pressed. In the emulator nothing happens at all (does the soft back button works at all in the emulator?) after pressing the soft back button.

On my smartphone the app is moved to background after pressing the soft back button and Android's home screen is shown. In my log file I can see that the action listener has never been called. My modified code on basis of the code listed above is as follows (I added some try-catches to see whether I get an exception):

        Command back = null;
        try {
            Log.p("addTask(): " + "Creating back command...");
            back = new Command("Back") {
                public void actionPerformed(ActionEvent ev) {
                    Log.p("addTask(): " + "Back button pressed!");
                }
            };
        } catch (Exception e) {
            Log.p("addTask(): " + e.toString());
        }


        try {
            if (back != null) {
                Log.p("addTask(): " + "Setting back command...");
                newForm.setBackCommand(back);
                Log.p("addTask(): " + "Back command set.");
            } else {
                Log.p("addTask(): " + "Back not set because back == null.");
            }
        } catch (Exception e) {
            Log.p("addTask(): " + e.toString());
        }

Any help available?

(By the way: How to capture volume up/down events?)

Community
  • 1
  • 1
Guzzer
  • 161
  • 5
  • Just found out the reason: I set the back command in the form before the form.show() command has been called. Setting the back command after calling form.show() made it working. – Guzzer Oct 12 '16 at 22:18

1 Answers1

0

Escape on the simulator simulates the back button, it should work fine before the form is shown but I'm guessing that something is overriding that code with another back button?

If this is an old GUI builder app then it is probably doing that.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65