Android Question Layout has too many views

tufanv

Expert
Licensed User
Hello,

After some research and talking to fellow forum members, I think I found my problem why my app has a high lag when the app is resumed : I have so many views in designer, it is too late to remove them from designer and add all of them via code, it will take too much effort and time. I am thinking of creating a second layout which has the splash screen only , and when user is coming back to app, I only want to draw this new layout and after all these completed I want to create all views with the old layout.

my question is, how can I deinitalize all the views in the big layout file when user minimzes the app so that I can load the new layout on activity resume,after all these operations complete I will load the huge layout.

Thanks
 

tufanv

Expert
Licensed User
and another question: is it possible to use "wait for" for activity .loadlayout completion ?
 

DonManfred

Expert
Licensed User
it is too late to remove them from designer and add all of them via code
it is a mistake to do that all by code

split the layout to multiplelayouts and load them when needed as @Alexander Stolte already mentioned.
 

tufanv

Expert
Licensed User
this will probably a dumb question but, now I have seperated to 5 layout files. When I load the second layout and when I am done with that views I want the second layout file to be removed so only 1st layout will remain initalized, how can i achieve this ? ( I can remove all views and load the first layout again but it takes around 4-5 seconds so it is not ideal )
 

emexes

Well-Known Member
Licensed User
When I load the second layout and when I am done with that views I want the second layout file to be removed so only 1st layout will remain initalized, how can i achieve this ?
You could tag the second layout's views, and then identify and delete them with a single pass using GetAllViewsRecursive.

Or make a note of them in an array when loading the second layout, and then remove them when you no longer need it. Mildly tricky because you have to differentiate them from stuff already loaded. If you can load the second layout first, that'd make it easier.

Probably the simplest solution is to put all of the second layout into a transparent screen-size panel, and then when you want to remove it, just move it off-screen eg change the panel's Left from 0 to 110%x.
 

techknight

Well-Known Member
Licensed User
Or if you want to increase performance and decrease memory consumption, remove the panel view. And re-add it in code when needed. (Depends on how big your project is)

Just keep in mind, you NEED to ensure that you test for a control being initialized before working with it. If label1.isinitialized = true then blahblahblah. All of my apps have a Refresh subroutine just for this reason, it refreshes the data in all of the controls. So when I change layouts, or pages, etc. I call that routine.

Otherwise it will crash from here to Antarctica without the layout being loaded.
 

tufanv

Expert
Licensed User
Or if you want to increase performance and decrease memory consumption, remove the panel view. And re-add it in code when needed. (Depends on how big your project is)

Just keep in mind, you NEED to ensure that you test for a control being initialized before working with it. If label1.isinitialized = true then blahblahblah. All of my apps have a Refresh subroutine just for this reason, it refreshes the data in all of the controls. So when I change layouts, or pages, etc. I call that routine.

Otherwise it will crash from here to Antarctica without the layout being loaded.
I used this method but I dont understand one thing. I remove the panels all child views and itself with this :

panelhisseekrani.RemoveAllViews
panelhisseekrani.removeview

after this I log
log(panelhisseekrani.isinitialized)

and it always gives true. How can it be initalized if it is removed ?
 

emexes

Well-Known Member
Licensed User
panelhisseekrani.RemoveAllViews
panelhisseekrani.removeview
It would seem that unlinking views from their parent, doesn't actually delete the view object.

To delete the view object, set any remaining references to it to null, eg:
B4X:
panelhisseekrani = Null
and sometime soon after, the Android/Java garbage collector will see that the view object is no longer used by anything, and reclaim the memory.

Good catch in spotting that the view still exists, though :)
 
Top