Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: how do i restart my android app when ondestroy event called?



Permlink Replies: 9 - Last Post: Jul 2, 2017 5:24 PM Last Post By: Francesco Marano
drama mix

Posts: 37
Registered: 6/27/12
how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 9:31 AM
i wanted to know how programmatically can i restart my android app when destroy event called .

i need to restart my app at some point but i dont know how can i restart my app with coding
Francesco Marano

Posts: 59
Registered: 11/18/02
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 11:16 AM   in response to: drama mix in response to: drama mix
Hello,

If you want your App to "bounce back" after destroy, try a PendingIntent.
See:
https://stackoverflow.com/questions/6609414/how-to-programmatically-restart-android-app

Not tested.

Regards,
Francesco
drama mix

Posts: 37
Registered: 6/27/12
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 2:01 PM   in response to: Francesco Marano in response to: Francesco Marano
Francesco Marano wrote:
Hello,

If you want your App to "bounce back" after destroy, try a PendingIntent.
See:
https://stackoverflow.com/questions/6609414/how-to-programmatically-restart-android-app

Not tested.

Regards,
Francesco

i have tried something like this

RIntent :=TJIntent.Create;
RIntent := TAndroidHelper.Context.getPackageManager().getLaunchIntentForPackage(TAndroidHelper.Context.getPackageName());
RIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_CLEAR_TOP);
RIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
TAndroidHelper.Context.startActivity(RIntent);

MainActivity.finish;

but the app did not restarted only closed
Francesco Marano

Posts: 59
Registered: 11/18/02
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 2:47 PM   in response to: drama mix in response to: drama mix
Hello,

I suggested to look the PendingIntent code.
Not tested.

from: https://stackoverflow.com/questions/6609414/how-to-programmatically-restart-android-app
var
  LIntent_Start : JIntent;
  LPendingIntent : JPendingIntent;
begin
  LIntent_Start := TJIntent.Create();
  LIntent_Start.setClassName(
      TAndroidHelper.Context,
      // TAndroidHelper.Activity.getClass.getSimpleName
      StringToJString( 'com.embarcadero.firemonkey.FMXNativeActivity' )
      );
 
 
  LPendingIntent := TJPendingIntent.JavaClass.getActivity(
      TAndroidHelper.Context,
      123456 {RequestCode},
      LIntent_Start,
      TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT
      );
 
  TAndroidHelper.AlarmManager.&set(
      TJAlarmManager.JavaClass.RTC,
      // ? System.currentTimeMillis() + 100
      System.DateUtils.MilliSecondsBetween(Now, 0) + 100,
      LPendingIntent
      );


Regards,
Francesco
drama mix

Posts: 37
Registered: 6/27/12
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 3:15 PM   in response to: Francesco Marano in response to: Francesco Marano
Francesco Marano wrote:
Hello,

I suggested to look the PendingIntent code.
Not tested.

from: https://stackoverflow.com/questions/6609414/how-to-programmatically-restart-android-app
var
  LIntent_Start : JIntent;
  LPendingIntent : JPendingIntent;
begin
  LIntent_Start := TJIntent.Create();
  LIntent_Start.setClassName(
      TAndroidHelper.Context,
      // TAndroidHelper.Activity.getClass.getSimpleName
      StringToJString( 'com.embarcadero.firemonkey.FMXNativeActivity' )
      );
 
 
  LPendingIntent := TJPendingIntent.JavaClass.getActivity(
      TAndroidHelper.Context,
      123456 {RequestCode},
      LIntent_Start,
      TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT
      );
 
  TAndroidHelper.AlarmManager.&set(
      TJAlarmManager.JavaClass.RTC,
      // ? System.currentTimeMillis() + 100
      System.DateUtils.MilliSecondsBetween(Now, 0) + 100,
      LPendingIntent
      );


Regards,
Francesco

should i replace this StringToJString( 'com.embarcadero.firemonkey.FMXNativeActivity' ) with my app name ?

also JPendingIntent is not declared

Edited by: drama mix on Jul 2, 2017 3:20 PM
drama mix

Posts: 37
Registered: 6/27/12
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 3:35 PM   in response to: Francesco Marano in response to: Francesco Marano
Francesco Marano wrote:
Hello,

I suggested to look the PendingIntent code.
Not tested.

from: https://stackoverflow.com/questions/6609414/how-to-programmatically-restart-android-app
var
  LIntent_Start : JIntent;
  LPendingIntent : JPendingIntent;
begin
  LIntent_Start := TJIntent.Create();
  LIntent_Start.setClassName(
      TAndroidHelper.Context,
      // TAndroidHelper.Activity.getClass.getSimpleName
      StringToJString( 'com.embarcadero.firemonkey.FMXNativeActivity' )
      );
 
 
  LPendingIntent := TJPendingIntent.JavaClass.getActivity(
      TAndroidHelper.Context,
      123456 {RequestCode},
      LIntent_Start,
      TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT
      );
 
  TAndroidHelper.AlarmManager.&set(
      TJAlarmManager.JavaClass.RTC,
      // ? System.currentTimeMillis() + 100
      System.DateUtils.MilliSecondsBetween(Now, 0) + 100,
      LPendingIntent
      );


Regards,
Francesco

I Have tried the following and still app closed and did not restarted

LIntent_Start := TJIntent.Create();
LIntent_Start.setClassName(
TAndroidHelper.Context,
StringToJString( 'com.embarcadero.firemonkey.FMXNativeActivity' )
);
 
LPendingIntent := TJPendingIntent.JavaClass.getActivity(
TAndroidHelper.Context,
123456 {RequestCode},
LIntent_Start,
TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT
);
 
TAndroidHelper.AlarmManager.&set(
TJAlarmManager.JavaClass.RTC,
System.DateUtils.MilliSecondsBetween(Now, 0) + 100,
LPendingIntent
);
 
MainActivity.finish;
 
Francesco Marano

Posts: 59
Registered: 11/18/02
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 4:09 PM   in response to: drama mix in response to: drama mix
Hello,

Tested:

var
  LPM : JPackageManager;
  LIntent_Start : JIntent;
  LPendingIntent : JPendingIntent;
  LMS : Int64;
begin
  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
      );
 
  // System.currentTimeMillis() is "wall clock time in UTC".
  LMS := DateTimeToUnix( Now, False {InputIsUTC} ) * 1000;
 
  TAndroidHelper.AlarmManager.&set(
      TJAlarmManager.JavaClass.RTC,
      LMS + 2000,
      LPendingIntent
      );
 
  TAndroidHelper.Activity.finish();


Source:
https://stackoverflow.com/questions/6609414/how-to-programmatically-restart-android-app

In the previous example, there was a mistake related to System.currentTimeMillis().
Also I add 2000 ms instead of 100 ms because FMX is slower than native.

Regards,
Francesco

Edited by: Francesco Marano on Jul 2, 2017 4:09 PM
drama mix

Posts: 37
Registered: 6/27/12
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 4:28 PM   in response to: Francesco Marano in response to: Francesco Marano
Francesco Marano wrote:
Hello,

Tested:

var
  LPM : JPackageManager;
  LIntent_Start : JIntent;
  LPendingIntent : JPendingIntent;
  LMS : Int64;
begin
  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
      );
 
  // System.currentTimeMillis() is "wall clock time in UTC".
  LMS := DateTimeToUnix( Now, False {InputIsUTC} ) * 1000;
 
  TAndroidHelper.AlarmManager.&set(
      TJAlarmManager.JavaClass.RTC,
      LMS + 2000,
      LPendingIntent
      );
 
  TAndroidHelper.Activity.finish();


Source:
https://stackoverflow.com/questions/6609414/how-to-programmatically-restart-android-app

In the previous example, there was a mistake related to System.currentTimeMillis().
Also I add 2000 ms instead of 100 ms because FMX is slower than native.

Regards,
Francesco

Edited by: Francesco Marano on Jul 2, 2017 4:09 PM

thank you very much this works but kinda slow
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 4:31 PM   in response to: drama mix in response to: drama mix
drama mix wrote:

TAndroidHelper.AlarmManager.&set(
TJAlarmManager.JavaClass.RTC,
LMS + 2000,
LPendingIntent
);

thank you very much this works but kinda slow

You might want to "tweak" the 2000 value to something smaller.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Francesco Marano

Posts: 59
Registered: 11/18/02
Re: how do i restart my android app when ondestroy event called?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 2, 2017 5:24 PM   in response to: drama mix in response to: drama mix
Hello,

Also, due the fact that DateTimeToUnix() approximates to 1 sec, there will be a random 0..1 sec delay.
I do not know if there is an equivalent function for System.currentTimeMillis(),
maybe try:
LMS := ..
LMS := LMS + System.DateUtils.MilliSecondOf( Now );


Regards,
Francesco
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02