Android Question Closing a app with code

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

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
 

aaronk

Well-Known Member
Licensed User
Longtime User
I copied the above code (from post1) from the B4A-Bridge as that is how it ended a app, so I thought I would use it in my app.

Like you see in computer software (Click File > Exit Application) I wanted to have the similar thing in my app.

You could achieve the same using ExitApplication, but, it is not recommended.
Why do you say it's not recommended?

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.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@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.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
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 ?
 
Upvote 0
Top