-4

Normally without having to code the android back button functions and lets you got back to the previous form. In my app i'm working on instead of going back it shuts down the application. I also tried handling the back button by code but this also didn't work, it ignored the code!

Here is the code I used to handle the backbutton:

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  var KeyChar: Char; Shift: TShiftState);
var
  FService: IFMXVirtualKeyboardService;
begin
  if Key = vkHardwareBack then
  begin
    TPlatformServices.Current.SupportsPlatformService
      (IFMXVirtualKeyboardService, IInterface(FService));
    if (FService <> nil) and (TVirtualKeyboardState.Visible
      in FService.VirtualKeyBoardState) then
    begin
      // Back button pressed, keyboard visible, so do nothing...
    end
    else
    begin
      // Back button pressed, keyboard not visible or not supported on this platform
      close;
    end;
  end;
end;

I used to use Delphi XE5 and now I use XE6 and hoped the problem was solved but regrettingly not. Also the above code is for the Delphi XE6 version, for XE5 it is slightly different.

UPDATE:

I've found a fix for my problem. But my delphi still reacts really strange. When I create a new project and add the files from my previous project i'm able to go back with the android backbutton. But as soon as I save the project again and then try to run it then it stops working.

Remi
  • 1,289
  • 1
  • 18
  • 52
  • Does this code work in a vanilla app? – David Heffernan Jul 02 '14 at 08:05
  • Yes, it does. Also when you look up delphi android backbutton you get this code. For other people it does work – Remi Jul 02 '14 at 08:09
  • 1
    So, the code in the question behaves as expected in a vanilla app, but does not in your app. At which point the question becomes entirely about the code in your app rather than the code in the question. The code in your app can only be seen by you. How can we help? It's time to make an SSCCE. – David Heffernan Jul 02 '14 at 08:17
  • Well the thing is that i have no clue what the problem is/can be. Also debugging doesn't help! I hope someone had the same problem and solved it! – Remi Jul 02 '14 at 08:28
  • How would anyone know that they'd had the same problem? Only you can see your application. The only way you'll get to the bottom of this is to make that SSCCE. Cut the app down to the smallest possible app that illustrates the problem. Hoping for magic isn't very likely to succeed. Debugging doesn't help? Of course it does. You just have to know how to do it right. Start by making that SSCCE. – David Heffernan Jul 02 '14 at 08:30
  • Yeah but my project is to big to do that and maybe someone encountered the same problem with the above code. The code is ignored but no errors or access violations – Remi Jul 02 '14 at 09:46
  • -1. In a comment you said that it works fine for another project you own, so clearly you know this is project specific. Why would anyone other then yourself be able to fix this issue without an overhaul of your entire code? – Peter Jul 02 '14 at 10:01
  • You guys don't seem to understand me. But i'll try to figure it out myself – Remi Jul 02 '14 at 10:14
  • Have it your way then. Good luck. – David Heffernan Jul 02 '14 at 10:20
  • 2
    Instead of attacking @Peter and myself with serial downvoting, it would be better if you followed our advice. The reason we cannot help is that you have not yet presented the information needed to describe, reproduce and isolate the problem. It's not a coincidence that both Peter and I are saying the same thing. All you need to do is make an SSCCE. When you do so I predict that you'll end up solving the problem yourself. I cannot understand why you ask for help and then ignore the sound help that is given to you. – David Heffernan Jul 02 '14 at 10:53
  • I'm not ignoring your help, only saying that I would like any suggestions. And the funny thing is that on the Embarcadero forum people are able to help and came up with a solution. – Remi Jul 02 '14 at 11:01
  • I don't know what result you wanted to achieve when you decided to attack myself and @DavidHeffernan with serial downvoting, I find it very very amusing. – Peter Jul 02 '14 at 11:36
  • That last statement about the forum (https://forums.embarcadero.com/thread.jspa?threadID=106139) would appear to be nonsense. Nobody can tell you about the specific issue with your application unless they can either see some code or read your mind. I can do neither. Perhaps I'm just deficient. – David Heffernan Jul 02 '14 at 12:15

3 Answers3

0

The back button has a special function in the android framework- onBackPressed(). I don't know if it was ported to Delphi, but I assume so. It won't come through that API

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Yes it is supposed to work in Delphi and the strange thing is that in another project of mine it does work. And the thing is I didn't have to do anything for that so – Remi Jul 02 '14 at 08:03
0

After creating a new project in a new directory and then adding all the files too the project the issue had dissapeared! The problem seemed to be somewhere in de dproj file. I tried too find it, but it was to big for me to locate it.

Remi
  • 1,289
  • 1
  • 18
  • 52
-1

You need to trap the "Back" keypress in your FormKeyUp procedure so that it isn't passed to the operating system after you act on it:

if (Key = vkHardwareBack) then
  begin
    Key := 0;
    { Do something else }
  end;
LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56