I have been using Activity.Finish before loading another Activity but just wondering if I used the following code would this also close my app completely, or will it cause errors?
It seems to work fine on my device, but want to know if this would work as well.
B4X:
Dim package As String
package = "my.app.here"
Dim sb As StringBuilder
sb.Initialize
Phone.Shell("ps", Null, sb, Null)
Dim m As Matcher
m = Regex.Matcher2("^[^ ]*\s+(\d+) .*" & package, Regex.MULTILINE, sb.ToString)
If m.Find Then
Log("Package found: " & package)
Phone.Shell("kill", Array As String(m.Group(1)), Null, Null)
End If
Would you say by doing the following is going to be a better way:
- Create a code module
- in the code module have a sub like:
CodeModule1:
B4X:
Sub CloseApplicationFully
If IsPaused(Activity1) = False Then Activity1.Finish
If IsPaused(Activity2) = False Then Activity2.Finish
StopService(Service1)
End Sub
Then in each Activity when you press the exit button (a button I will create and add to the page) run CodeModule1.CloseApplicationFully to close the app fully ?
I want a way to fully close the app with a single button press regardless if I forget to use Activity.Finish when moving between activity's etc.
In VB6.0 you can use End and it will close the application completely, so I was hoping to somehow do the same thing in my app and that's where I come across the B4A-Bridge code to see how the app fully closes when you press the stop button in the B4A IDE.
You can call ExitApplication but you should make sure that the services are first stopped. Otherwise Android may recreate the process (as if the process has crashed).
@Erel: You used to be very adamant about using ExitApplication to close the app, regardless whether there are services involved or not. Has there been a change in recent OS versions that make you be more accepting of its use. I am getting mixed messages as to whether there is a place for it in coding. If there is, please let us know under what conditions. This is the most prevalent question that is asked in the forum.
I don't recommend using ExitApplication. You should let Android decide when and if to kill the process. If you do want to kill it yourself then you should make sure that all the services are stopped.
There are cases where ExitApplication may be appropriate. Such as when working with USB devices.
So if I was to stop the service ( StopService("Service") ) then call ExitAppliction or use the code from post 1 then I should be fine and the app will be fully closed ?