0

First of all, I know a little bit about restarting an app. but that's when it is for windows. In this case, I need to make this for an app that is from android. I couldn't find a solution for it that works in Delphi. Just found this from @Mihai Limbășan I quote:

Perhaps you should think outside the box. Instead of futzing with the mutex/instance logic, you could simply create another executable that waits for your app to close then starts it again. As an added bonus, you can later use this mechanism to, for example, update some of your main app's binaries. It's also much easier to run it elevated instead of maintaining different integrity levels inside the same app, etc.

But have no idea how this works or even where to start... Every tip, code sample, or maybe other solution to restart an app will be appreciated.

EDIT

after some questions here are some pieces of code from the procedure.

First. after you choose for example the language 'English and push the button save this happens

  Inifile := TIniFile.Create(fPath);
  try
  Inifile.WriteString('Instelling','ip',edit5.text);
  Inifile.WriteString('Instelling','user',edit6.text);
  Inifile.WriteString('Instelling','pixels',edit3.text);
  Inifile.WriteInteger('Instelling','language',Combobox2.ItemIndex);
  fGebruiker := Edit6.Text;
  fFotoformaat := StrToInt(edit3.Text);
  finally
  FDConnection1.Params.Values['server']:=edit5.Text;
  FDConnection1.Connected := True;
  inifile.free;
  End;

with this code, I fill an inifile with data as you see also the item index of the Combobox for the language.

on this point i restart the app manually so the right language is chosen by this code:

procedure TfmMain.FormShow(Sender: TObject);
VAR
param : string;
inifile : tInifile;
begin
 


if (System.SysUtils.fileexists(fPath)) then
     Begin

         begin
          Inifile := TIniFile.Create(fPath);

              try
              if not (Inifile.ReadString('Instelling','ip','default')='default') and not (Inifile.ReadString('Instelling','Gebruiker','default')='default')then
                try

                edit5.text := Inifile.ReadString('Instelling','ip','default');
                edit6.text := Inifile.ReadString('Instelling','user','default');
                Edit3.text := Inifile.ReadString('Instelling','pixel','400');
                combobox2.ItemIndex := IniFile.ReadInteger('Instelling','language',1);
                fpixel:= StrToInt(edit3.Text);
                fuser:=edit6.text;
                FDConnection1.Params.Values['server']:=edit5.Text;
                 taal := 'NL';


                //find language settings
                  if combobox2.ItemIndex=0 then
                  begin
                    language:= 'NL'
                  end;

                  if combobox2.ItemIndex=1 then
                  begin
                    language:= 'ENG';
                  end;

                 if language='ENG' then
                 begin
                 vertalerENG.vertaler('ENG');
                 end;
              end;
            end;
         end;
      end;

the VertalerENG is a function that is fired if the language parameter is ENG and change all of the captions to English.

the problem is that nothing is changed till i restart the app.

  • Can you clarify why you need to restart the app in Android? If you want to simply reload something in App, can't you just reload it without restarting? – Lynch Chen Oct 20 '21 at 13:38
  • @LynchChen the user can choose between languages and when the language is changed I want to restart the app so the buttons has the right captions –  Oct 20 '21 at 13:40
  • This is incorrect, you shouldn't restart the App, you should make a method to update the UI with something like `myButton.setText()` and call it when the language is changed. It is not common to restart for changing language in Android, this is not Windows. – Lynch Chen Oct 20 '21 at 13:43
  • @LynchChen if you choose the language from the Combobox on the change event I fire a function where all the buttons change their text to the language that is selected but there is nothing changed and when I restart the app then it's changed. that's why I thought this would help. –  Oct 20 '21 at 13:47
  • @LynchChen But i think what i did wrong. I wrote a line in the inifile that looks at which language it is. but i only read that string when the app starts up. –  Oct 20 '21 at 13:53
  • I'm not sure if we're on the same page. There's no Combobox and iniFile in Android, it is only for Windows, are you talking about `Spinner` and `Sharedpreferences` in Android? And you should review the problem of why the language is not changed again. Restarting the App is not a good option to consider. – Lynch Chen Oct 20 '21 at 14:02
  • @LynchChen In delphi i make apps with comboboxes and infiles. it's a multidevice. But i fixed the problem without restarting the app. –  Oct 20 '21 at 14:05
  • @Aaron Can you please provide [mcve] for your use case. It should be possible to update language live in the Delphi Android application. Also make sure you pinpoint exact problem - do you have problem with saving/reading the settings or you have a problem with applying captions of the controls at runtime. Restarting the application is not exactly an option on Android. – Dalija Prasnikar Oct 20 '21 at 14:35
  • @DalijaPrasnikar check the edited question is this what you mean? –  Oct 20 '21 at 15:03
  • Yes. More or less. You didn't show code you run on ComboBox change. You just need to run your code that changes the language on controls or other logic inside that event handler. Did you run your vertalerENG.vertaler('ENG') inside ComboBox change handler? Also, you would need additional code that can change language back from English to Dutch. – Dalija Prasnikar Oct 20 '21 at 15:18
  • @DalijaPrasnikar there is no code for the combobox. i use `Inifile.readstring` to see wich itemindex the combobox is that i saved and from that info i run the VertalerENG like ` if combobox2.ItemIndex=1 then begin Taal := 'ENG'; end; if taal='ENG' then begin vertalerENG.vertaler('ENG'); end; ` –  Oct 20 '21 at 15:25
  • OK, then you need to call all needed logic in the code when you trigger saving your ini file. If you don't run the code that changes language (VertalerENG) then nothing will happen. FormShow runs only when application is show on the screen (on restart or when you bring the application to the foreground) You need to run some logic similar tp one in the FormShow (not all, just the code related to the language settings. – Dalija Prasnikar Oct 20 '21 at 15:47

2 Answers2

0

If you want restart app programmatically, This code work fine for me and you can set time elapse before restart app

uses
Androidapi.Helpers,Androidapi.JNI.GraphicsContentViewText,Androidapi.JNI.App,
System.DateUtils;
...

procedure RestartApp;
     {$IFDEF ANDROID}
    var  LPM : JPackageManager;
      LIntent_Start : JIntent;
      LPendingIntent : JPendingIntent;
      LMS : Int64;
     {$ENDIF}
begin
    {$IFDEF ANDROID}
      LPM := TAndroidHelper.Context.getPackageManager();
      LIntent_Start := LPM.getLaunchIntentForPackage(
          TAndroidHelper.Context.getPackageName()
          );
      LIntent_Start.addFlags( TJIntent.JavaClass.FLAG_ACTIVITY_CLEAR_TOP );
    
      LPendingIntent := TJPendingIntent.JavaClass.getActivity(
          TAndroidHelper.Context,
          223344 {RequestCode},
          LIntent_Start,
          TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT
          );
      LMS := DateTimeToUnix( Now, False {InputIsUTC} ) * 1000;
    
      TAndroidHelper.AlarmManager.&set(
          TJAlarmManager.JavaClass.RTC,
          LMS + 10000,
          LPendingIntent
          );
          // TAndroidHelper.Activity.finish();
        Application.Terminate;
    {$ENDIF }
end;
A.K.D.
  • 90
  • 9
-2

If changing the language is your only concern then i would suggest changing the locale of the application. You only need to restart the activity if you're using all the strings correctly from strings.xml

You can see it here how to change the locale of application programatically.

Change app language programmatically in Android

  • 3
    This is Delphi Android application that works in very different way than standard Android application. This answer does even remotely answer this question. – Dalija Prasnikar Oct 20 '21 at 14:39