Android Question Open and close B4XPage

Sergey_New

Well-Known Member
Licensed User
Longtime User
In B4A, when changing any parameter (for example, font size), I did it like this:
Activity.Finish
StartActivity(Me)
The newly opened Activity is loaded with the new parameter.
How can this be done with B4XPage?
 

jansondrew

New Member
You can achieve a similar effect in B4XPages by calling B4XPages.ClosePage followed by B4XPages.ShowPage. However, instead of restarting the page entirely, it's more efficient to update the parameter directly and then call a method like UpdateUI on the current page to reflect the changes (like font size). This avoids unnecessary overhead and gives a smoother user experience. If needed, you can also use B4XPages.MainPage to access shared methods or data between pages.
 
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
When you use B4XPages the state of the views on the pages are kept.
If you switch to another page and then back the views settings of the page are reused.
The font size for instance is the same as before the switch.
You should try it out with a test project.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
So where is it?
There is no point in continuing. You did not answer the question, and for some reason you did not provide the B4XPages code, even if you knew how to solve the problem. That's it, I put a period.
 
Upvote 0

valerioup

Member
Licensed User
Longtime User
Reload page:
Private Sub Reload_Click
    Root.RemoveAllViews
    B4XPage_Created(Root)
End Sub

This forces a page reload, it's not very nice and Erel probably won't like it but it works
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
That is a good solution but imperfect, as there may be instructions in the B4XPage_Created that you don't want to execute more than once.

Alternative:
B4X:
Private Sub Reload_Click
    Root.RemoveAllViews
    Root.LoadLayout("...")
End Sub
 
Upvote 0

valerioup

Member
Licensed User
Longtime User
I deliberately called the create method because it's not just a matter of rereading the layout but of completely reloading the page, with all the parameters etc.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
That is a good solution but imperfect, as there may be instructions in the B4XPage_Created that you don't want to execute more than once.
I deliberately called the create method because it's not just a matter of rereading the layout but of completely reloading the page, with all the parameters etc.
For example, it's common practice (not good, I'd say) to initialize pages (B4XPages) and add them to the collection in B4XPage_Created.
This operation should only be performed once.
 
Upvote 0

valerioup

Member
Licensed User
Longtime User
In fact, I wrote that it is not an elegant solution, but it works, it was to respond to post no. 1 in which it reported how to obtain the same result as closing an activity and reopening it with b4xpages, something I would never do because I would implement an UpdateUi with the modification of the parameters.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
valerioup, your example works.
In my case, after rebooting, I need to set a new font size in B4XComboBox, while preserving its contents.
Taking into account LucaMs comment, tell me how to implement UpdateUi?
I attached the example. I commented out the code that I don't like.
 

Attachments

  • Project.zip
    4.7 KB · Views: 33
Upvote 0

valerioup

Member
Licensed User
Longtime User
Try this:
Private Sub Button1_Click
'    B4XComboBox1.cmbBox.Clear
'    B4XComboBox1.SetItems(lst)
'    B4XComboBox1.SelectedIndex=1
'    B4XComboBox1.cmbBox.TextSize=14
'    Root.RemoveAllViews
'    B4XPage_Created(Root)
'    B4XComboBox1.cmbBox.TextSize=14
    
    UpdateUI
    
End Sub


Sub UpdateUI
    IteraTutteLeView
    'Imposta i caratteri
    B4XComboBox1.cmbBox.TextSize=14
    Spinner1.TextSize=14
End Sub

Sub IteraTutteLeView   
    For Each v As B4XView In Root.GetAllViewsRecursive
        'Log($"View: ${v} - Tipo: ${v.Tag}"$)     
        If v Is Button Then
            'non cambio nulla
        Else
            ' Esempio: se vuoi fare qualcosa su tutti i bottoni
            If v Is Label Then
                'Dim lab As Label = v
                'test con cambio colore
                v.TextColor=Colors.Red
                v.TextSize=14
            End If
        End If
    Next
End Sub

Unfortunately, the spinner doesn't allow you to change the font size directly, but by iterating on the page components you can simply change the font (I added the red color only to make the screen update visible). This is an alternative approach but it works for your case.
 

Attachments

  • testOK.zip
    10.8 KB · Views: 38
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Thanks to everyone for participating and advice!
Reloading the page is not required yet due to the solution found for changing the font size of B4XComboBox.
B4X:
Private Sub Button1_Click
    For Each v As B4XView In Root.GetAllViewsRecursive
        If v Is Button Then
        
        Else If v.Tag Is B4XComboBox Then
            Dim cmb As B4XComboBox=v.Tag
            cmb.cmbBox.TextSize=14
        Else if v Is Label Then
            v.TextSize=14
        End If
    Next
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…