Handling Hard Button vkHardwareBack

2.7k views Asked by At

In my application I use TTabControl with 3 tabs: Main Tab (TabItem1) with 3 buttons: Button1 will take me to TabItem2, Button2 to TabItem3, and Button 3 to TabItem3.

I handle FormKeyUp Event to Control the Navigation and:

If the key pressed is vkHardwareBack then if the ActiveTab is the TabItem1, I popup a message asking if the user wants to quit the application. If the answer is yes, I close the application and if not nothing happens. this part is working just fine!

But if the ActiveTab is TabItem2 or TabItem3 then I want the application to go back to the Main Tab (TabItem1) by firing TTabChangeItem standard action with properties:

  • Tab: TabItem1
  • Direction: tdReversed
  • Transaction: ttSlide

But this is not happening. When the user is pressing the vkHardwareBack while the ActiveTab is TabItem2 or TabItem3 the application goes to background and the home screen is shown.

Any Idea what am I doing wrong?

And here is a the FormKeyUp procedure:

procedure TfMain.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
begin
  if Key = vkHardwareBack then
    if TabControl.ActiveTab = TabItem1 then
      if MessageDlg('Are you sure you want to Exit?', TMsgDlgType.mtWarning,
           [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0) = mrYes then
        MainActivitiy.finish
     else
       ChangeTabAction1.Execute; // I tried here also: TabControl.ActiveTab := TabItem2; but still the same results
end;
1

There are 1 answers

4
Remy Lebeau On BEST ANSWER

Maybe try something like this (untested):

procedure TfMain.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  if Key = vkHardwareBack then
  begin
    Key := 0;
    if TabControl.ActiveTab = TabItem1 then
    begin
      if MessageDlg('Are you sure you want to Exit?', TMsgDlgType.mtWarning, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0) = mrYes then
      begin
        MainActivitiy.Finish;
      end;
    end else
    begin
      ChangeTabAction1.Execute;
    end;
  end;
end;