Implement onDestroy()

warwound

Expert
Licensed User
Longtime User
Hi.

Is it possible that B4A could implement the Activity onDestroy() method?

I was hoping to create a B4A library around the Android mapsforge library.
Unfortunately, a mapsforge MapView must be used within a mapsforge MapActivity.
I wanted to create a library much like my OSMDroid library where a mapsforge MapView could be added to a B4A Activity just as any other view is added.
I don't want to create a pre-compiled MapActivity (in Eclipse) and add it to a B4A project.

So i've looked as the mapsforge MapActivity to see where it differs from the standard Activity class.
MapActivity overrides the Activity onDestroy, onPause and onResume methods.
I can take the MapActivity code from onPause and onResume and add it to the B4A library and then the B4A project can call that code in it's Activity onPause and onResume methods.

If B4A implemented the onDestroy method then i could also take the MapActivity onDestroy code and add it to the B4A library and then the B4A Activity could call it onDestroy.

I could then do away with the mapsforge MapActivity and make a standalone mapsforge MapView that could be added to a B4A Activity - just as other B4A Views are added to a B4A Activity.

A workaround would be to call the code that must be executed in onDestroy in the onPause method.
I think that would work but it'd be inefficient - MapViews would be destroyed when sometimes there is no need to destroy them.
For example let's say i have started B4A Activity #1 displaying a MapView and the user clicks a button to start B4A Activity #2 and then returns to B4A Activity #1.

Activity #1 would be paused but not destroyed when Activity #2 is started, and then Activity #1 would be resumed when Activity #2 is exited.
There is no need to destroy the MapView in Activity #1 when it is paused - when Activity #1 is resumed, the MapView would no longer exist.

The workaround might work but it would be an inelegant hack - interfering with the standard Activity lifecycle.

Anyway - enough detail - is there any way a future B4A update could implement onDestroy()?

Martin.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is possible to add onDestroy. The reason it wasn't added is that in most cases it is not needed and the activities life cycle is confusing enough without it.

onDestroy is not guaranteed to run at all (Android may just kill the entire process).

One possible solution for your case is to only call the destroy code when UserClosed is true (in Activity_Pause).
 

warwound

Expert
Licensed User
Longtime User
That's an idea but won't be reliable enough.

The mapsforge MapView runs one or more Threads and these must be paused or terminated depending on the state of the Activity.

I had an idea yesterday though, the mapsforge MapView extends the ViewGroup class and the ViewGroup class has an onDetachedFromWindow method which it inherits from the View class.

I shall look at adding code to the ViewGroup's onDetachedFromWindow to pause and/or terminate the Threads.

Martin.
 
Top