Android Question Issues with Removing Pages and Waiting for B4XPages to Load

Javaneh

Member
Hi everyone,

I’ve recently started transitioning from Activities to B4XPages, and I’ve encountered a few issues:

  1. Problem with Removing Pages
    Let’s say I have two pages. I navigate to the second page using the following code:
B4X:
B4XPages.ShowPage("p2")

On the second page, I want to remove it and go back to the first page using:

B4X:
B4XPages.ShowPageAndRemovePreviousPages("MainPage")

However, the problem is that the second page doesn’t seem to be removed properly. When I navigate to the second page again, it shows the same data as before. To fix this, I have to use the following code:

B4X:
Root.RemoveAllViews
B4XPages.ShowPageAndRemovePreviousPages("MainPage")

Why is this happening? Is the second page not actually being removed?


  1. Problem with Waiting for B4XPage to Load
    Let’s say, due to the app’s algorithm, the MainPage of the B4XPages library hasn’t been loaded yet. From another class or activity, I want to navigate to the MainPageActivity using:
B4X:
StartActivity("MainPageActivity")

After this line, I want to immediately execute a B4XPages Sub. For example:

B4X:
B4XPages.MainPage.ReceiveParsedData("test")

Initially, my code threw an error stating that the B4XPage wasn’t loaded yet.

To solve this, I added a Sleep(100) before the line to give the B4XPage time to load. However, when the page finishes loading, the sleep process seems to halt, and the next line (e.g., B4XPages.MainPage.ReceiveParsedData("test")) doesn’t execute at all.

Here’s a simplified example:

B4X:
StartActivity("MainPageActivity")
Sleep(100)
B4XPages.MainPage.ReceiveParsedData("test")

The problem is that when the B4XPage loads, the sleep is interrupted, and the Next lines aren’t executed.

What causes this behavior, and how can I properly handle it?

Thank you!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
However, the problem is that the second page doesn’t seem to be removed properly. When I navigate to the second page again, it shows the same data as before. To fix this, I have to use the following code:
This is how B4XPages works. The page class instances are never destroyed by the framework. If you want to reset the state then add a method that clears everything.

The problem is that when the B4XPage loads, the sleep is interrupted, and the Next lines aren’t executed.

What causes this behavior, and how can I properly handle it?
This is one of the problems of activities that B4XPages solves. They are paused once a different activity is resumed. Things will be simpler if you change all your activities to B4XPages, however you can use CallSubDelayed(Main, "SomeSub", ...) to start the activity and then call a sub in that activity. Call the pages sub from the activity sub.
 
Upvote 0

Javaneh

Member
This is how B4XPages works. The page class instances are never destroyed by the framework. If you want to reset the state then add a method that clears everything.


This is one of the problems of activities that B4XPages solves. They are paused once a different activity is resumed. Things will be simpler if you change all your activities to B4XPages, however you can use CallSubDelayed(Main, "SomeSub", ...) to start the activity and then call a sub in that activity. Call the pages sub from the activity sub.

Thank you, Erel, for your response.

so what is the Remove function does in the following code? What is its exact purpose?

B4X:
B4XPages.ShowPageAndRemovePreviousPages("PageName")
 
Upvote 0
Top