Android Question B4XPages: how to remove a page from memory?

wimpie3

Well-Known Member
Licensed User
Longtime User
I know you can return back to a previous page and hide the current one by doing B4XPages.ShowPageAndRemovePreviousPages("previouspagename").

However, that just seems to hide the current page and remove it from the stack. It still sits in memory, because when I show the page again, I can see the previous contents.

When your app has 50 pages, and they've all been opened once by the user, won't that be an issue for the memory of the mobile phone? Is there a way to remove a page completely from memory and load it back again when needed at a later stage?
 

LWGShane

Well-Known Member
Licensed User
Longtime User
Unsure of the official method, or if there is even an official method, but here's a work-around: In B4XPage_Appear, call Root.RemoveAllViews and after that call Root.LoadLayout.

B4X:
Private Sub B4XPage_Appear
    Root.RemoveAllViews
    Root.LoadLayout("YourLayoutFile")
End Sub
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
If pages are declared on the MainPage, so:
B4X:
B4XPages.MainPage.usbPage = null
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
However, that just seems to hide the current page and remove it from the stack. It still sits in memory, because when I show the page again, I can see the previous contents.
That's one of the many advantages of B4XPages projects.

If absolutely necessary, I would create a Public Sub that removes all views from Root and reloads the layout (unless there's an easier way that I can't think of at the moment).

PS. You could duplicate B4XPage_Created (B4XPage_Created2 or B4XPage_reCreated) and use that (still adding the code to remove the views)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
That's one of the many advantages of B4XPages projects.

If absolutely necessary, I would create a Public Sub that removes all views from Root and reloads the layout (unless there's an easier way that I can't think of at the moment).

PS. You could duplicate B4XPage_Created (B4XPage_Created2 or B4XPage_reCreated) and use that (still adding the code to remove the views)
B4X:
Public Sub ClearPage
    Root.RemoveAllViews
    B4XPage_Created(Root)
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
won't that be an issue for the memory of the mobile phone?
If that is what concerned you.

On the other side, some users feel annoying to wait the page to load the same data again. Some users don't want their input lost when back to that page.

If I understand correctly, even we "clear" the memory, it is still waiting for Java garbage collector to come collect the "garbage". The OS will manage the memory and kill it if it thinks necessary.
 
Upvote 0
Top