トップへ(mam-mam.net/)

バックボタンで終了させない

検索:

「バックボタンで終了させない」

バックボタンで終了させない

回答

ソース

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs
  , FMX.Layouts, FMX.Memo, System.Math.Vectors, FMX.Types3D,
  FMX.StdCtrls, FMX.Controls3D, FMX.MaterialSources, FMX.Objects3D,
  FMX.Viewport3D 
  ,FMX.Platform//TPlatformService
  ,FMX.VirtualKeyboard //IFMXVirtualKeyboardService
  ,FMX.Platform.Android //MainActivity
  ;

type
  TForm2 = class(TForm)
    Memo1: TMemo;
    Viewport3D1: TViewport3D;
    Model3D1: TModel3D;
    Model3D1Mat01: TLightMaterialSource;
    Model3D2: TModel3D;
    Model3D2Mat01: TLightMaterialSource;
    Light1: TLight;
    Dummy1: TDummy;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
      Shift: TShiftState);
  private
    { private 宣言 }
  public
    { public 宣言 }
    function HandleAppEvent(AAppEvent:TApplicationEvent;AContext:TObject):boolean;
    procedure Log(s:String);
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}
procedure TForm2.Log(s: string);
begin
  Memo1.Lines.Add(TimeToStr(Now) + ': ' + s);
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  Dummy1.AnimateFloat('RotationAngle.Y',360,2);
end;

procedure TForm2.FormCreate(Sender: TObject);
var
  AppEventService:IFMXApplicationEventService;
begin
  //Viewport3D1.Visible:=True;
  //IInterface(AppEventService)

  if
    TPlatformServices.Current.SupportsPlatformService(
      IFMXApplicationEventService
    )
  then
  begin
    try
      AppEventService:=
        IFMXApplicationEventService(
          TPlatformServices.Current.GetPlatformService(
            IFMXApplicationEventService
          )
        );
    except
      AppEventService:=nil;
    end;
    //AVD(仮想環境)でデバッグモードで下記を実行すると、非アクティブ時等に落ちる
    //iOSでもAndroidでもちゃんと動くらしい
    //当然Windowsの場合は以下が実行されない
    if AppEventService<>nil then
      AppEventService.SetApplicationEventHandler(HandleAppEvent);
  end
  else
  begin
    //Windowsの場合は以下が表示される
    Log('Application Event Service is not supported.');
  end;

end;

procedure TForm2.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
var Keyboard:IFMXVirtualKeyboardService;
begin
  //Androidのbackキーを処理する
  if Key=vkHardwareBack then
  begin
    keyboard:=TPlatformServices.Current.GetPlatformService(
      IFMXVirtualKeyboardService
    ) as IFMXVirtualKeyboardService;
    if TVirtualKeyboardState.Visible in keyboard.VirtualKeyboardState then
      exit;
    MainActivity.moveTaskToBack(True);
    Key:=0;

  end;
end;

function TForm2.HandleAppEvent(AAppEvent: TApplicationEvent;
  AContext: TObject): boolean;
begin
{
Finished Launching
Become Active

Entered Background
Will Become Inactive
Will Become Inactive


Will Become Foreground
Became Active

}

  case AAppEvent of
    TApplicationEvent.FinishedLaunching:
    begin
      //起動完了
      Log('Finished Launching');
    end;
    TApplicationEvent.BecameActive:
    begin
      //フォーカスを取得-------------------------------------
      Log('Became Active');
      Viewport3D1.Visible:=True;
    end;
    TApplicationEvent.WillBecomeInactive:
    begin
      //フォーカス喪失-------------------------------------
      Log('Will Become Inactive');
      Viewport3D1.Visible:=False;
    end;
    TApplicationEvent.EnteredBackground:
    begin
      //裏に回った
      //Log('Entered Background');
      //Viewport3D1.Visible:=False;
    end;
    TApplicationEvent.WillBecomeForeground:
    begin
      //表に回った
      Log('Will Become Foreground');
    end;
    TApplicationEvent.WillTerminate:
    begin
      //終了しようとしている
      Log('Will Terminate');
    end;
    TApplicationEvent.LowMemory:
    begin
      //メモリが少ない
      //Log('Low Memory');
    end;
    TApplicationEvent.TimeChange:
    begin
      //iOSのみ時刻が変わった(日が変わったり夏時間などで)
      //Log('Time Change');
    end;
    TApplicationEvent.OpenURL:
    begin
      //iOSのみURLを開いた
      //Log('Open URL');
    end;
  end;
  Result := True;

  {
  //Androidの場合。IOSは違う。Delphiはこれらを吸収して上記のようになっているらしい
  onCreate  ⇒onStart
  onRestart ⇒onStart
  onStart   ⇒onResume or onStop
  onResume  ⇒onPause
  onPause   ⇒onResume or onStop
  onStop    ⇒onRestart or onDestroy
  onDestroy

  }
end;

end.