Android Question B4XPages - creating views in different pages, event problem

mmanso

Active Member
Licensed User
Longtime User
Hi All,

For a project specific reason, I'm trying to create views by code in different B4XPages. Everything works ok except the fact that I can't catch the Click event on the correct page.

I've created a zip file of a project demonstrating the issue.

Basically I've MainPage and Page2 (two pages). Each page have a button that switch from MainPage to Page2 and vice-versa.

When we enter Page2, two buttons are created, one in MainPage and other in Page2. They're well created because when we navigate to both pages, we see them created. I'm creating one button in MainPage and another in Page2 (when I enter Page2) using this code:

B4X:
Private Sub CreateButtonInDifferentPages

    ' Add button to page1
    Dim btnPage1 As Button
    btnPage1.Initialize("page1")
    btnPage1.TextSize = 20
    btnPage1.Gravity = Gravity.CENTER
    btnPage1.TextColor = Colors.White
    btnPage1.Color = Colors.Blue
    btnPage1.Text = "Button in Page1"
    B4XPages.MainPage.Root.AddView(btnPage1, 400dip, 200dip, 200dip, 100dip)

    ' add button to page2
    Dim btnPage2 As Button
    btnPage2.Initialize("page2")
    btnPage2.TextSize = 20
    btnPage2.Gravity = Gravity.CENTER
    btnPage2.TextColor = Colors.White
    btnPage2.Color = Colors.Red
    btnPage2.Text = "Button in Page2"
    Root.AddView(btnPage2, 400dip, 200dip, 200dip, 100dip)

End Sub

I had to make Root from MainPage Public so I can access it from Page2 to add the button to it.

Now, my problem begins.

I've this in MainPage:

B4X:
Private Sub page1_Click
    Dim ctrl As B4XView = Sender
    Log("########## page1_Click in Page1 - " & ctrl.Text)
End Sub

And this in Page2:

B4X:
Private Sub page2_Click
    Dim ctrl As B4XView = Sender
    Log("########## page2_Click - " & ctrl.Text)
End Sub

I was expecting that when I press the button in page1 I would see the page1_Click event being raised from MainPage and when I clicked on the button in the second page I would see the event page2_Click in page2 being fired. That doesn't happen. If I add this to Page2:

B4X:
Private Sub page1_Click
    Dim ctrl As B4XView = Sender
    Log("########## page1_Click in Page2 - " & ctrl.Text)
End Sub

I'll see that this event is fired in Page2 event after I've added it to the Root B4XView of MainPage.

Is there any way I can achieve this? I mean, create the views in the correct page and make the events to be fired on the page I've created them?

This is a very specific issue but I hope any advanced users might have the knowledge to help me out...

Thanks in advance.
 

Attachments

  • B4XPages-Test.zip
    11.5 KB · Views: 90

mmanso

Active Member
Licensed User
Longtime User
Meaning?

i have all views defined in a file per each page. Im trying to create a generic sub that loads the views everytime i enter a page and I’m having that problem. That little project just demonstrates the issue I’m having.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

mmanso

Active Member
Licensed User
Longtime User
Hi there,

Thank you both. Knowing how it's supposed to work will allow me to work on a solution for my particular situation.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…