How can one class instance handle UI events from views in 2 different activities?

knutf

Active Member
Licensed User
Longtime User
Hi all!
Erel say that "Android UI elements hold a reference to the parent activity. As the OS is allowed to kill background activities in order to free memory, UI elements cannot be declared as process global variables (these variables live as long as the process lives). Such elements are named Activity objects. The same is true for custom classes. If one or more of the class global variables is of a UI type (or any activity object type) then the class will be treated as an "activity object". The meaning is that instances of this class cannot be declared as process global variables." (http://www.b4x.com/forum/basic4andr...orials/18626-classes-tutorial.html#post106825)

It looks as a way around this is to let activityA add an instance of an "activity object" custom class to a process global list in a Code module. Then you can let:
-the custom class instance add views to activityA
-the custom class store a reference to the view in a class global variable
-the custom class handle the events from the views in a class member sub.​

If an other activity, activityB is started:
-The activityB can access the class instance stored in the process global list
-The class instance can add views to activityB
-But the class instance can not handle the events from the views added to activityB​

Is there a way to go so the class instance can handle events from views in more than one activity? For example, is it possible to let the class instance be created outside the context of an activity?
 

agraham

Expert
Licensed User
Longtime User
Upvote 0

stevel05

Expert
Licensed User
Longtime User
What exactly are you trying to achieve here, it sounds like you can do what you want straightforwardly with a class, which can add views to an activity and deal with their events.

You can pass these events back to the activity that holds the reference to the class using the Me keyword when you initialize the class, and different Activities can have their own instances of the class without and issue which can then load a layout or anything else you want to do with it.

I think you may just be looking at it the wrong way round.
 
Last edited:
Upvote 0

knutf

Active Member
Licensed User
Longtime User
If you activate a "CreateViews" metode of a customClass like this
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim CustomClass as object
   For Each CustomClass In Main.ListOfCustomClasses
      CallSub2(CustomClass,"CreateViews",main.activity.panel)
   Next
End sub

Then the custom class can make a view in the activity like this:
B4X:
Public Sub CreateViews(P As Panel)
   Dim ET As EditText
   ET.Initialize("ET")
   P.AddView(ET,10dip, 10dip, 70dip,20dip)
End Sub

If you do it like this, the event sub must be a metode of the custom class.

You can pass these events back to the activity that holds the reference to the class using the Me keyword when you initialize the class...

How do you pass an event back to the activity using the me keyword?
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
As far I can see in your example the two activities have their own instance of the class CreateViews.

My question was how two activities can share the same instance of one class that handle UI events from both activities.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It could be possible pass the instance to a different activity as a parameter, but it is difficult to know the best way to proceed without knowing exactly what you are trying to acheive. You could also pass the ListOfCustomClasses as a parameter to the class initialization routine and handle the looping in there, or manage the whole thing within the class.

Are you trying to create a class that will handle the creation and management of views that they are the same for multiple activities?
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
You can't. You can't share an instance of a class between two activities if the class handles UI elements. This is a consequence of the way Android is designed.

If you keep an instance of a View that instance will have a reference to its Activity which in turn holds references to other views and resources. If Android decides to destroy that Activity the garbage collector will not be able to reclaim what is likely to be a large amount of memory because of that one reference which will be of no use and probably cause an error if it is accessed.

Basic4android makes it as hard as possible to do this in order to avoid memory leaks which plagued early Android applications when device memory sizes were less than nowadays and programmers hadn't got the message. Unfortunately in Java it is easy for a novice programmer to create a memory leak just by a little oversight.
Avoiding memory leaks | Android Developers Blog
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
Hi!

Thank you for giving me a bit more understanding.:) I have decided to do it in an other way. I use one activity that create and keep the instances of my classes of activity object type in the global sub. Then the activity has two panels. By controlling the visibility property of the panels I can make almost the same effect as with creating two activities. My only concern is that the menu will be the same for each of the panels.
 
Last edited:
Upvote 0
Top