Skip to main content
October 20, 2010
Question

Back button in Android

  • October 20, 2010
  • 11 replies
  • 4860 views

I need to be able to "capture" the back button to work inside of my application.  Currently, I have the following code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler_2);


function fl_KeyboardDownHandler_2(event:KeyboardEvent):void

{

// Start your custom code

if(event.keyCode == Keyboard.BACK)

            {

                gotoAndPlay(1);

            }


// End your custom code

}

The code works, but the application still closes.  I would like to find a way to make sure the back button only works a certain way inside my application.  Thanks!

This topic has been closed for replies.

11 replies

Inspiring
January 30, 2011

Others including myself have run into a situation where the keyboard event was not heard at times when attached to stage. If stage.focus = stage; corrected it, its probobly the same issue.

What does work 100% is to add the listener to native application.

example:

import flash.desktop.NativeApplication;


NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, buttonPressed);

hope this helps

January 14, 2011

I think what I'm doing should work but its not. After I install it, the first time I run the app with this code, then navigate to this frame then press the back button on my nexus one, the app closes. Then if i re-open the app then go to this frame and then press the back button it works(and by working i mean take me back to my "home" frame. Then if i open it again it doesn't work (just exits the app like the first time). It's like it's remembering the event prevent default the second time around but the first time It's just "setting it up" or something like that.

Also, just would like to point out i'm a long time android user and developer so i'm well aware how an android app should be setup and navigated through. I'm just trying to give this actionscript3 and FPCS5 thing a shot and add some basic functionality to have the back button take users back to the home menu screen i made.

stage.addEventListener(KeyboardEvent.KEY_DOWN, buttonPressed);

function buttonPressed(event:KeyboardEvent):void

{

if (event.keyCode == Keyboard.BACK)

{

event.preventDefault();

stage.removeEventListener(KeyboardEvent.KEY_DOWN, buttonPressed);

gotoAndStop("home");

}

}

Participating Frequently
January 14, 2011

I recommend stepping through the app in the debugger, or sprinkling some trace statements to see what is going on.

Participant
December 9, 2010

Also, this might help someone: if you are using KEY_UP, it won't work since on KEY_DOWN it is already exiting.

Participating Frequently
October 26, 2010

Call event.preventDefault() in the body of your event handler.

Note that you SHOULD let the back button take the user out of the application when they've returned to the initial view of your app. "Trapping" a user would be contrary to expectation on the Android platform.

YopSolo75
Participating Frequently
November 24, 2010

perfect thx