I need, when the user clicks the "Close" button, to destroy the application. If you specify Action = caFree, the application remains hanging in memory, not destroyed, the Destroy methods in the application and service do not work:

procedure TfmMain.FormClose(Sender: TObject; var Action: TCloseAction); begin DM.LocationSensor.Active := false; Action := TCloseAction.caFree; end; 

If you destroy the application in the smartphone through the "Latest Applications", then the application is destroyed, the application service restarts, and continues to work - that's what I need.

Help please understand how to properly destroy the application when you close the main form?

Solutions suitable for sending Intent where needed, or calling native Android methods.

    3 answers 3

    As written here :

    You can use the activate finish () method, provided that this last activation on the application stack closes, or you can kill the process completely regardless of the amount of activation in it.

    Or use this: android.os.Process.killProcess (android.os.Process.myPid ());

    • There is no possibility to use the killProcess method in FireMonkey regularly, but the idea is clear. - Pax Beach

    Use

     ANativeActivity_finish(System.DelphiActivity); 

    And do not forget to connect Androidapi.NativeActivity

      1. In PlatformSDK we find the class android \ os \ Process.java
      2. Compile it in Process.class
      3. We pack in jar
      4. Convert JAR to DEX
      5. Combine with Classes.dex
      6. Using Java2OP.exe we make a wrapper for Delphi - Androidapi.JNI.Os.Process.pas
      7. Import the resulting library into the project.

      Call the code:

       TJProcess.JavaClass.killProcess(TJProcess.JavaClass.myPid); 

      All this could be replaced by calling the code.

       var LJO: JObject; begin TJActivityManager.Wrap((LJO as ILocalObject).GetObjectID).killBackgroundProcesses(TAndroidHelper.Context.getPackageName); end;