Ali Pouradam
Ali Pouradam

Reputation: 1

Disable Default function of "Back" button for Android

I have tried SO MANY methods to PREVENT the default function of Back button in Android but no success

This is one of the codes I have used with no success:

stage.addEventListener(KeyboardEvent.KEY_UP, optionsKey, false, 0, true);

function optionsKey(ke: KeyboardEvent): void {
    if (ke.keyCode == Keyboard.BACK) {
        ke.preventDefault();
        ke.stopImmediatePropagation();
        //Do my own code below...
    }
}

I am using Flash Cs6 , Air 3.2 for Android , Testing it on LG Google nexus 4.

When I use the flash simulator, it works fine! Means the Back Button, let the App go back to previous step (as I expect it to do)

But after I publish the apk (Aspect ratio: Auto , Auto orientation , Render mode: GPU), as soon as I press BACK, it minimize my app!

(means when i press the Back button on my phone, the App minimize down (not close!). when I re-activate it/click on it, I see the app has actually gone one step BACK (as I programmed and expected), but I just CANT STOP the default action of MINIMIZING/De-activating my app after pressing Back button.

Anyone have any "Fla" file that I can see it in ACTION please?!

Because I have used many-many codes and no success yet

thanks and looking forward for your any kind of help...

Upvotes: 0

Views: 8023

Answers (6)

Brijesh
Brijesh

Reputation: 255

 //Override onBackPressed    
 public void onBackPressed() {
    return; //Do nothing!
 }

Upvotes: 1

Panoman
Panoman

Reputation: 397

NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true)

function onKeyDown(event:KeyboardEvent):void
{
if( event.keyCode == Keyboard.BACK )
{
event.preventDefault();
event.stopImmediatePropagation();
//handle the button press here. 
   }
}

Note that if you’ve set stage.displayState = FULL_SCREEN, no keyboard events are sent to your app! Use stage.displayState = FULL_SCREEN_INTERACTIVE instead!

Upvotes: 1

RajeshVijayakumar
RajeshVijayakumar

Reputation: 10622

You can disable the back button functionality. By removing the following method call as follows:

@Override
public void onBackPressed() {

    System.out.println("back pressed");
    if(isValid) {
        // This super.onBackPressed() performs the default back button function
        super.onBackPressed();
    } else {
        Toast.makeText(SpO2TestActivity.this, "You can't close this screen", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 0

Ravi Kumar Soni
Ravi Kumar Soni

Reputation: 11

Actually the android back key work with two functions by default. one is with key UP and another is key DOWN

so to disable default commands of back button and add our own we have to write the code like this.

its good if we remove/disable the key default key Down function and only work with KEY Up function so to remove/disable we will addEventListener Keyboardevent.KEY_DOWN to nativeapplication and simply disable the feature by event.preventDefault(); we will then again addEventListener Keyboardevent.KEY_UP to nativeapplication and simply disable the feature by event.preventDefault(); and then add our own commands to it ! you can do it vice versa to ...but this is preferrable cz if the key is pressed down the count of down could be more than one and could effect your commands accordingly.

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

NativeApplication.nativeApplication.addEventListener( KeyboardEvent.KEY_UP, android_backUp );


function android_backDown(e:KeyboardEvent)
{
        switch (e.keyCode)
       {
        case Keyboard.BACK :
        e.preventDefault();
        break;
       }
}


function android_backUp(e:KeyboardEvent)
{
  switch (e.keyCode)
  {
    case Keyboard.BACK :
    e.preventDefault();
    // YOUR CODE
    break;
  }
}

Upvotes: 1

T_V
T_V

Reputation: 17580

Override onBackPressed()

 @Override
public void onBackPressed() {
  //keep it blank
}

The default implementation simply finishes the current activity, but you can override this to do whatever you want.

Upvotes: 0

Ankur Aggarwal
Ankur Aggarwal

Reputation: 2220

Can you not use this simple code?

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
}

Upvotes: 1

Related Questions