Android Question Auto Close and Restart

William Hunter

Active Member
Licensed User
Longtime User
In my app the user makes changes that only take effect once the app is restarted. I would like to find a way that would close, and then restart the app on user request, such as with a MsgBox. Can this be done, and if so is there a bit of sample code that would illustrate just how it's done?

Best regards :)
 

William Hunter

Active Member
Licensed User
Longtime User
I have never dealt well service modules but I suppose this can be the only way, although to close definitely an app you should close its services.

But I have a question: are you sure it's necessary to close the app to get the changes? An example?
Thank you for your reply. I am trying to restart the app to affect a change in orientation. I start with the Main Module Sub making the changes. These changes are stored to be affected on the next restart:
B4X:
Sub ButtonOrientation_Click
If RadioButtonPortrait.Checked = True Then
OriMode = 1
'Log("ChkBoxPortraitRotButt: " & OriMode)
Else If RadioButtonLandscape.Checked = True Then
OriMode = 0
'Log("ChkBoxLandscapeOriButt: " & OriMode)
End If
kvs.PutSimple("orientation", OriMode)
OrientationPanel.Visible = False
RestartApplication
End Sub
This makes a call to the Main Module Sub RestartApplication:
B4X:
Sub RestartApplication
Activity.Finish
StartService(AutoRestart)
End Sub
This makes a call to the Service Module - AutoRestart
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
StartActivity(Main)
End Sub
Sub Service_Destroy
End Sub
This works for me. But, it all seems so simple that I think there may be something missing in my approach. Any help much appreciated.

Best Regards :)
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Could you call a sub from Activity_Create and from ButtonOrientation_Click to get the same behavior?
Yes, I could eliminate the service, and simply change the code in Sub RestartApplication to this:
B4X:
Sub RestartApplication
Activity_Create(False)
End Sub
This would affect the change in orientation I desire. But, I had thought it would be better to finish the activity first. Hence the reason I used the service. Is Activity_Create(False) called from Sub RestartApplication all that is required? :confused:

Best regards
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
That makes two of us. Thanks for your interest Luca. I will have to look for more information re the use of services. o_O

I tried your suggestion:
B4X:
Sub RestartApplication
Activity.Finish
Activity_Create(False)
 End Sub
As predicted this just closes the app. Nothing after Activity.Finish is in play.
Best regards :)
 
Last edited:
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
If you only change the orientation, why don't you just call Activity_pause followed by _create and _resume? Those event-subs should hold allthe needed info to restore the activity with the correct settings. Did you read this tutorial from Erel https://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/#content
Thank you opus. That is a very worthwhile read. I've bookmarked it, and will likely read it quite a few more times. It seems there are a few ways to accomplish my objective. As with many other things in life, it's whatever floats your boat.

Best regards :)
 
Upvote 0
Top