App is looping all the time

Shay

Well-Known Member
Licensed User
Longtime User
Very Very strange - hard to explain, but I will try

following my last posts, that I was unable to free RAM and CPU and had to use both commands:
Activity.Finish
ExitApplication

which solved the problem
I have also 3 menu item
Activity.AddMenuItem

problem is:
if I click on menu button called exit that runs:
B4X:
Sub mnuExit_click
 
 Dim result As Int
 result = Msgbox2("Are you sure you want to exit?" ,"Exit","Yes","No","",LoadBitmap(File.DirAssets,"exit.ico"))
 If result = DialogResponse.Positive Then 
 
   If Main.ScreenSaver = True Then
    Main.Awake.ReleaseKeepAlive
   End If
    StopService(ServiceModule)
    Activity.Finish
    ExitApplication ' Kill the app

 End If
 
End Sub
so, if I click on it from main module - app is exisiting

if I am moving to different module using one of the other button (for example moving for settings layout)
and from their I execute the same code, the app is exiting and STARTING once again !!!!

To make it even starnger:
if I put breaking point on the line ExitApplication and press F8, it will exit ok
if I remove the breaking point it will loop again (close the app and start the app)

Any idea what is going on ???? :BangHead:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Calling StopService sends a message to the service and makes it stop. You are calling ExitApplication which kills the process immediately. Therefore the service (and also the activity) are not stopped correctly.
Are you using Basic4android v1.6? I don't expect the service to get started again in v1.6.

The solution is to avoid calling ExitApplication or to wait for the service to stop and then call ExitApplication.
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
I am running 1.5
Again the reason for the Exit Application is - if i am not using it, app remains in RAM/CPU
is it also 1.5 bug?
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Its not a bug. Its the way Android is designed. Apps don't close out completely when they 'exit', it's more like they minimize. When memory gets low, Android will close out 1 or more apps that haven't ran in a while to free enough memory for a new app to run. This is normal Android behavior. Its the way its supposed to be. Erel is the creator of B4A. Consider losing the ExitApplication statement as he suggests.

Sent from my DROIDX using Tapatalk
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
I would have get this answer, but
I see 2 problems with that:
CPU still not zero
Device never go to sleep... until I kill it
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
If Check_Screen_Saver.Checked = True Then
Main.Awake.KeepAlive(True)
Else
Main.ScreenSaver = False
Main.Awake.ReleaseKeepAlive
End If
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
b.t.w I removed the ExitApplication from Setting module
but when I pressing the exit (activity.finish)
it is moving back to main module (I am not finishing the main, before moving to other menus - since I need it running all the time)
and not exiting the app
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
Hi

I did what you suggested and it closed the app
so I removed the "ExitApplication" and it is still exiting

BUT the CPU is still consuming ~2% cpu
is there a way to debug what is it?
:BangHead:
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
I manage to find a way to replicate the problem:

1. Create new project
2. Turn on GPS
3. Turn On Wireless
4. Use B4A-Bridge
5. Use the following code:

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

Dim GPS1 As GPS

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Dim Button_DialNow As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)

If FirstTime Then

GPS1.Initialize("GPS")

Activity.LoadLayout("Main_Menu")

End If


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub GPS_LocationChanged (Location1 As Location)

End Sub

Sub GPS_UserEnabled (Enabled As Boolean)

End Sub

Sub GPS_GpsStatus (Satellites As List)

For i = 0 To Satellites.Size - 1
Dim Satellite As GPSSatellite
Satellite = Satellites.Get(i)
Next
End Sub

Sub Button_DialNow_Click
GPS1.Stop
Activity.Finish
End Sub


6. Run it from debugger, and press the button (Button_DialNow) to exit
7. Go to Active task, see that CPU is still consuming, and kill the app (this is not the problem - I guess that B4A is doing something)
8. Stop debugger (if not stopped)
9. Press the home in the device, and run the app from the icon
10. press the Button_DialNow to exit
11. go to active tasks and see that your app is still consuming CPU


if you remove one part of the above it is not consuming any CPU
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
You should set a process global "exit" variable. In Activity_Resume of the main activity check its value and if it is true close the activity. This will allow you to close all activities.

:BangHead:
I could not win this game. Several activities with 3 services.
Common sub in a separate code module, stopping all services and setting a global FullFinish = True.

At resuming each activity if FullFinish = True, then Activity.Finish.
And just shows previous opened before activity. :BangHead:
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Each Activity must have this Sub that kills all services and Activity.Finish.
Then application is finished fully OK :)
 
Upvote 0
Top