Android Question Class Life cycle

iCAB

Well-Known Member
Licensed User
Longtime User
I am trying to understand the life cycle for a class /class module, I am hoping that someone can answer my question below:

1. Say I created a Class that raises Class Events, for example ( GPS _NMEA )
2. Say I declare an instance of the class in Activity_Create ( I am assuming this makes it a local object to Activity_Create and that it should be terminated with Termination of Activity_Create Sub)
3. how come the events for the class keep getting raised while the program is running?, when does the instance of the class get terminated?

Thanks in advance
 

stevel05

Expert
Licensed User
Longtime User
You shouldn't depend on the garbage collection to stop your processes, you should add a termination method and call that when you want to switch it off.

As I understand it, when there are no more active pointers to a class, it will be available for garbage collection as with any other variable. This may not happen immediately, and in fact, may not happen as long as the app is running if it doesn't need to (memory is not an issue). I haven't seen a definition of what should happen in the meantime, but as you have stated, it obviously continues to run.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
I was under the impression that local objects are automatically terminated at the end of the Sub where they are being declared.
Can someone show me the correct syntax to do so.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As I said, this is based on my understanding, which may not be 100% correct. How to terminate it will depend on what your class is doing, you will need to stop any process it is running, also try removing any listeners you have added.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I was under the impression that local objects are automatically terminated at the end of the Sub where they are being declared.
Objects are cleared from memory when there are no more live references to these objects.

In this case the internal GPS object holds a reference to your class. You need to explicitly stop the GPS for it to be cleared.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Assuming that I have a Class that:
1. builds an HTTP call
2. Issues the HTTP call
and assuming that I want HTTPJob complete to Call the SameClass
In other words, pass the Class instance as the Target for the call below

B4X:
Public Sub Initialize (Name As String, TargetModule As Object)
    JobName = Name
    target = TargetModule
End Sub

Would that be ok and if so what is the proper syntax

Thank you
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Im not sure i understand it right.

If you want the class xy to make http calls and you want the class xy get the httpresults (using the JobDone sub) you just need to give

ME

as the parameter when initializing the httpjob.

You may want to have a look at my Dropbox HTTPApi V2 Class. search the forum for it...

It is using httputils and the results are handled by the class.

The parsed results are then passed back to the calling activity.
 
Upvote 0
Top