Hardware back button in Android

611 views
Skip to first unread message

Baltasar García Perez-Schofield

unread,
Oct 3, 2012, 4:38:53 AM10/3/12
to codenameone...@googlegroups.com
Hi, there!

What about the hardware back button not working by default?

Is this related to this.setBackCommandEnabled( true ); ?
What do I have to do in order to enable that?

Nikhil

unread,
Oct 3, 2012, 11:25:59 AM10/3/12
to codenameone...@googlegroups.com
Use Form.setBackCommand(myCommand);

Baltasar García Perez-Schofield

unread,
Oct 5, 2012, 5:16:00 AM10/5/12
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Hi!

On Wednesday, October 3, 2012 5:25:59 PM UTC+2, Nikhil wrote:
Use Form.setBackCommand(myCommand);

Thank you, but, how? I mean, it does not appear to have an attribute or something relating to the form or the builder, so the call below does not even compile. How could I exit the application, anyway?

protected void beforeMain(Form f)
    {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.beforeMain(f);
   
        this.setBackCommandEnabled( true );
        Command back = new Command( "Back" ) {
            public void actionPerformed(ActionEvent evt)
            {
                showPreviousForm();
            }
        };
        f.setBackCommand( back );
      
    }

I've also tried to create a listener and insert it in the StateMachine class, but it seems that there is no way to link a command that way (no set listener method).

Thanks,

-- Baltasar

Shai Almog

unread,
Oct 5, 2012, 11:23:35 AM10/5/12
to codenameone...@googlegroups.com, nikhil...@hotmail.com
The code is good, you just need to adapt it for your needs. If you want to exit just invoke Display.getInstance().exitApplication() in your actionPerformed. Although I'm guessing you would want to call minimizeApplication() instead.

Baltasar García Perez-Schofield

unread,
Oct 6, 2012, 4:41:35 AM10/6/12
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Hi!


On Friday, October 5, 2012 5:23:35 PM UTC+2, Shai Almog wrote:
The code is good, you just need to adapt it for your needs. If you want to exit just invoke Display.getInstance().exitApplication() in your actionPerformed. Although I'm guessing you would want to call minimizeApplication() instead.

Thanks !

-- Baltasar
 

Ronald Jones

unread,
Feb 1, 2013, 9:57:44 PM2/1/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Shai

I've deployed a couple of the sample demo apps to my android phone and found them all to be missing what seems to be a basic functionality for an app created for the Android platform: back-button functionality.
To not provide the default expected behavior seems like an oversight in this regard.  While I see that there is a programatic solution mentioned here to the issue, it is odd to me that your own demos lack this basic functionality required of all Android applications.  The first thing new programmers are going to review are your demos, and the first thing any Android app developer is going to see is that even your own Codename One Live program doesn't support default back-button behavior , much less the demos!  This really seems incredible to me!  I do hope you would consider turning on some default back-button code which then developers are free to override/extend and customize as they see fit afterwards.  But, out of the box, on the Android platform, the back button needs to be hooked up on ANY app.  It is definitely not something merely left to the programmers discretion to leave out.

Why would anyone create an app for the Android (or other) platform which did not play by the rules?  To win over mindshare, the apps created with this tooling must be at least as great as possible using the native tooling on the various platforms. Support of a back button is so universal to the Android platform ... it boggles the mind that this wasn't addressed by somebody in the year of beta testing before the 1.0 release.

IMO, all of your demos and the "Live" app, all need to be revised to include basic back-button functionality on Android, and whatever equivalent is on the other platforms. By default, code should be included which handles minimizing the app automatically as you suggested.  If a developer needs other behavior, they can go edit that default behavior.  But default behavior that is normal and expected on a platform should be implemented (not just available to do as an exercise) in every new app project created as part of it's template.

--ronald

bry...@webbtide.com

unread,
Feb 2, 2013, 12:34:12 AM2/2/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Ronald - what is the default behaviour you see as missing in the demos ?

Shai Almog

unread,
Feb 2, 2013, 3:48:11 AM2/2/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Back button works on every single one of these demos.
I'm assuming you mean back button to minimize the application?

That's something we should probably add, I don't notice it myself since I can't stand this specific behavior in android. Where I accidentally double click back and minimize the app.

Ronald Jones

unread,
Feb 2, 2013, 1:51:03 PM2/2/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Aloha

To clarify, I wrote, in part, "I've deployed a couple of the sample demo apps to my Android Samsung Note II phone and found them all to be missing..."; therefore, the "all" I am referring to was a limited test to the "couple" of samples I had actually deployed to device.

Specifically, the Codename One "Live" app, the NativeDemo app, and the ZXing app, do not exhibit "normal" human interface guidelines for an Android back-button, e.g. the back-button doesn't function as expected in the Android environment.  

I'm sure developers, even Java developers, will want to create apps on all the various platforms that at least meet a minimum expected functionality on those platforms by their typical end users.  There is a reason that Apple, and Google, specify GUI guidelines.  Navigation in these ecosystems is part of an apps responsibility to play by the rules to meet the expectations of the end-users so that they don't get lost trying how to figure out how to go back to the previous screen or previous application they were running.

Newer Android apps now provide a pop-up which says if they press the back-button a second time they will exit the program after they have pressed it the first time.  This is to prevent the unwanted behavior of accidentally exiting from the app when you didn't mean to do so.  At other times, when the program itself has gone further down in it's own navigation of it's own "activities, intents, or fragments", the back-button serves as basic navigation backward.

Google's Android does perhaps not have the best navigation paradigm in the world, and has even grown more complicated with the introduction of "fragments" and the possibility of going "up" instead of "back", but it is the world in which Android apps must learn to live within (even Java Codename One apps!).  To do otherwise, means an app developer has just made their app less usable, less friendly, less understood, by the end-users trying to hit back-buttons and getting no response.

Cross platform apps write once, run anywhere is such a great idea; however, apps that behave well, at least for the Android and iOS side of things is really a requirement to compete successfully.

Shalom.

Ronald Jones

unread,
Feb 2, 2013, 2:21:44 PM2/2/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Here is a YouTube video I created describing the issue:



On Friday, February 1, 2013 10:48:11 PM UTC-10, Shai Almog wrote:

Chen Fishbein

unread,
Feb 2, 2013, 2:31:03 PM2/2/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Hi,
you do understand that the physical back command is mapped in CodenameOne? 
If the form set it's back command like this  f.setBackCommand( back ); it will be mapped to the physical button on Android.
Now the fact that the back button does not minimize the codenameone live app or the demos is simply a bug in the app.

Ronald Jones

unread,
Feb 2, 2013, 3:26:36 PM2/2/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Yes, I agree.  It's a "bug in the app."

Furthermore, IMO, new developers should be insulated from making this kind of mistake by 
hooking up by default the back button behavior expected on the Android platform.

The New Project template code should include the minimum default behavior that is appropriate and expected for each platform.
If then the developer decides they want different behavior for the button because they are developing a game or whatever,
then that's fine; however, the default template code (or GUI designer/builder generated code) should include the implementation 
of the minimum UI/UX human interface guidelines for the platforms being deployed to, out of the box.

Shai Almog

unread,
Feb 3, 2013, 3:21:57 AM2/3/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
Sure.
I'll one up you.
By the end of the week this issue will be resolved for all the applications without any action by you, we will deploy a fix to the build server which will be applied across the board.

Its our policy to make everything act "right" by default when possible.

bry...@webbtide.com

unread,
Feb 3, 2013, 9:20:24 PM2/3/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
In a post on another thread, you mention that Apple probably wouldn't accept an app with an explicit back (aka exit) command on the home form. Is there accepted/defined behaviour on iOS for exiting an app which you'll include in this mod ?

Shai Almog

unread,
Feb 4, 2013, 2:10:39 AM2/4/13
to codenameone...@googlegroups.com, nikhil...@hotmail.com
The patch I made is only relevant to devices with a physical (or simulated physical) back button so it won't do anything for iOS.
All iOS devices just exit via the home button. This always worked and you don't need to do anything in your app in order for this to work (works on Android, RIM and every device that has a home button or end button).
Reply all
Reply to author
Forward
0 new messages