2
votes

In a Codename one app, how do we establish the back command from the native, the hardware back button on the device, to go back to previous form? Most androids have a native back button, but when I press it in the app, it does not go in the previous form, it minimizes the app. I have tried through constants in the .res file, I have not found a relevant option. (The red underlined button, bottom left in this image)

Added: All forms are created using the new GUI builder.

the red lined

3

3 Answers

3
votes

I was also struggling with the back command, here is how it works (in my opinion) :

Let's say that you have FormA and FormB. You launch FormB from FormA and when you press back button in FormB, FormA shows back.

To do this : In FormA, declare this command :

Command back = new Command("Back") {
    @Override
    public void actionPerformed(ActionEvent evt) {
        showBack();
    }
};

Still in FormA, in the actionEvent of the button or something else that launches FormB :

public void onFormActionEvent(com.codename1.ui.events.ActionEvent ev) {
    Form formB = new FormB();
    formB.getToolbar().setBackCommand(back);
    formB.setBackCommand(back);
    formB.show();
}

It works for me and it's the only way I manage to run the back command.

1
votes

If you use something like:

myForm.getToolbar().setBackCommand("", e -> showPreviousForm());

It would work correctly by implicitly placing the arrow on the top left and handling the physical back button.

If you just want the physical back button on devices that have it you can do that using:

myForm.setBackCommand(new Command("") {
    public void actionPerformed(ActionEvent ev) {
        showPreviousForm();
    }
});

Notice I'm referring to the hardward button as it looks the same for the app as the on-screen back button on the bottom.

1
votes

try this

for example this for your own button

 button.setOnClickListener(new View.OnClickListener() { 
@Override public void onClick(View view) {
 onBackPressed(); 
} });

this for use device back button

@Override
public void onBackPressed() {
    finish();
}

this for use your own button

button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             finish();
         }
     });