Question

If this has already been visited here on SO, please point me to it, cause I cant seem to find it. Having said that:

Using the standard delphi application events, as well as the Mobile app lifecycle events handling , i am trying to find the best spot to read and write a INI file?

as I test, i created a demo app with a button which increments a count variable and displays it in a show message

procedure TfrmMain.Button1Click(Sender: TObject);
begin
 inc(Count);
 ShowMessage(IntToStr(Count));
end;

In the main form's OnCreate even, I read the inifile

procedure TfrmMain.FormCreate(Sender: TObject);
var
 Ini: TIniFile;
begin
 Ini := TIniFile.Create( TPath.GetDocumentsPath + PathDelim + 'fortysixtozero.ini' );
 try
  Count    := Ini.ReadInteger( 'Main', 'Count', 0 );
 finally
  Ini.Free;
 end;
end;

Now, knowing that a mobile app can have different states, i am wondering where the best place is to write the ini file?

Was it helpful?

Solution

Best states to save the application state or store settings is "aeEnteredBackground". I used the delphi FMX event here. You should also check the "aeWillBecomeInactive" and "aeWillTerminate" events, but the first one is the most relavant. The application enters background when another application is opened or yours is closed (they are not terminated right away).

Check this article.

The code to listen for events looks like this:

function TfMain.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean; 
begin  
  case AAppEvent of
    aeFinishedLaunching: ;
    aeBecameActive: ;
    aeWillBecomeInactive: ;
    aeEnteredBackground: ;
    aeWillBecomeForeground: ;
    aeWillTerminate: ;
    aeLowMemory: ;
    aeTimeChange: ;
    aeOpenURL: ;   
  end;

  Result := True; 
end;

To attach the listener you use the platform services:

  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(SvcEvents)) then
    SvcEvents.SetApplicationEventHandler(HandleAppEvent);

Just add "FMX.Platform" to your uses clause.

OTHER TIPS

In Delphi XE7, there is an "OnSaveState" event for forms. This is the preferred place to save application data since it will execute when an iOS app goes into the "background" state. The documentation is quite helpful... search for "save state".

Here is my code in the main form's OnCreate handler:

procedure TMainWindow.FormCreate( Sender : TObject );

var
   IniFile : TIniFile;

   Metric : BOOLEAN;

   IniFileName : STRING;

   Reader : TBinaryReader;

begin
   fInitializing := True;

   SaveState.StoragePath := TPath.GetLibraryPath;

   if SaveState.Stream.Size > 0 then begin
      Reader := TBinaryReader.Create( SaveState.Stream );

      try
         Metric := Reader.ReadBoolean;

         vMetricUnits.IsChecked := Metric;

         SetSliderLimits( Metric );

         Temperature := Reader.ReadDouble;

         Dewpoint := Reader.ReadDouble;

         Humidity := Reader.ReadDouble;

         WindSpeed := Reader.ReadDouble;

      finally
         Reader.Free;
      end;
   end

   else begin
      Metric := False;

      vMetricUnits.IsChecked := Metric;

      SetSliderLimits( Metric );

      Temperature := 70;

      Dewpoint := 70;

      Humidity := 100;

      WindSpeed := 0;
   end;

   SetMetricUnits( cMetricUnits );

   fInitializing := False;

   WriteTrackbarCaptions;

   CalculateTemperatures;
end;

And here is the code in the form's OnSaveState handler:

procedure TMainWindow.FormSaveState( Sender : TObject );

var
   Writer : TBinaryWriter;

begin
   SaveState.Stream.Clear;

   Writer := TBinaryWriter.Create( SaveState.Stream );

   try
      Writer.Write( cMetricUnits );

      Writer.Write( Temperature );

      Writer.Write( Dewpoint );

      Writer.Write( Humidity );

      Writer.Write( WindSpeed );

   finally
      Writer.Free;
   end;
end;

I've tested this on both the iPad and in Windows and it works on both platforms. Doing it this way completely avoids the use of the .ini file, however it does create a somewhat oddly named .tmp file in the Windows version. I assume that an equivalent file is also created on the iPad.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top