iOS Question Confused on initializing a page

Dennis Glowack

Member
Licensed User
I have declared a Private variable on a certain page. Its value is set to 1. The variable changes when I press either of 2 buttons that can either increment it or decrement it. For argument's sake, the variable stops on the value of 5 when I exit the page. When I reselect the page, that value of 5 is retained. I don't know why. The page is being re-initialized. I am not checking to see if it was previously initialized. I'm just reinitializing it in the same way that it was first created. I tried removing the page from the stack when I exit it; but it still initializes and retains the value of 5. What's happening here? I just want the whole page to go away without any remnant of it and recall it. Clearly, I don't understand something about how pages are managed.

Thanks in advance, forum.
 

Dennis Glowack

Member
Licensed User
This is the advantage of B4XPages: they are never destroyed, so the only routine that is executed after closing and reopening them is the B4XPage_Appear (Initialize and B4XPage_Created will be executed only once).

Thanks for your reply. However, I WANT the page to be DESTOYED. I am not currently using B4XPages.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Just create a new "Default" project.
The OP is asking about B4i native pages, not B4XPages.

1. Everything is simpler with B4XPages. If this is a new project then switch to B4XPages.
2.
he page is being re-initialized. I am not checking to see if it was previously initialized.
Please post the relevant code.
 
Upvote 0

Dennis Glowack

Member
Licensed User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    
    Private PageChartsAndGraphs As Page
    Private btnNext, btnPrevious As Button
    
    Private xui As XUI
    
    'My Charts
    Private PieChartEmployers As xChart
    Private PieChart2Employers As xChart
    Private PieChartCashCredit As xChart
    Private LineChartAvgTips As xChart
    Private BarChartBestTipDays As xChart
    Private StackedBarChartActualVsMin As xChart
    Private BarChartEarningsPerHour As xChart
    Private PieChartTipRetention As xChart
    Private PieChartMileage As xChart
    Private BarChartBestTipHours As xChart
    Private BarChartDeliveriesByHour As xChart
    Private BarChartDeliveriesByDay As xChart
    
    Private pnlCharts As Panel
    Private labelExplanation As Label
        
    Private nCashTipRatio As Double 
    Private nCreditTipRatio As Double 

    Private SelectedChart = 1 As Int  'Never resets to 1 after page is exited, then redisplayed'

    Private iad As InterstitialAd

End Sub

Public Sub ShowChartsAndGraphs
    PageChartsAndGraphs.Initialize("PageChartsAndGraphs")
    
    ResetLayout  'Localize
    
    'Set gradiant color to panel
    GlobalFunctions.SetTheme(pnlCharts, False)
    
    Main.NavControl.ShowPage(PageChartsAndGraphs)

    ShowChart(SelectedChart)
    
    If Main.UseAds Then
        Private iad As InterstitialAd
        If Main.UseTestAds Then
            iad.Initialize("iad","ca-app-pub-3940256099942544/4411468910")  'Testing ID
        Else
            iad.Initialize("iad","ca-app-pub-3033958332466628/7153345605")
        End If
        iad.RequestAd
    End If
End Sub

Private Sub btnNext_click
    SelectedChart = SelectedChart + 1
    If SelectedChart >= 1 And SelectedChart <= 12 Then
        ShowChart(SelectedChart)
    End If
End Sub

Private Sub btnPrevious_click
    SelectedChart = SelectedChart - 1
    If SelectedChart >= 1 And SelectedChart <= 12 Then
        ShowChart(SelectedChart)
    End If
End Sub

Private Sub PageChartsAndGraphs_Disappear
    Main.NavControl.SetPagesStack(Array (MainScreen.PageMain))  'Tried to kill chart page to no avail'
    SelectedChart = 1   'Without resetting to chart 1, page reappears at the same chart number that was displayed when page was exited'
    MainScreen.CloseMenu
End Sub

Private Sub ShowChart(pChart As Int)
    'Hide all charts except the next one
    PieChartEmployers.Visible = False
    PieChart2Employers.Visible = False
    PieChartCashCredit.Visible = False
    LineChartAvgTips.Visible = False
    BarChartBestTipDays.Visible = False
    StackedBarChartActualVsMin.Visible = False
    BarChartEarningsPerHour.Visible = False
    PieChartTipRetention.Visible = False
    PieChartMileage.Visible = False
    BarChartBestTipHours.Visible = False
    BarChartDeliveriesByHour.Visible = False
    BarChartDeliveriesByDay .Visible = False

    Select pChart
        Case 1
            btnPrevious.Visible = False
            btnNext.Visible = True 
            PieChartEmployers.Visible = True
            CreatePieDataEmployers
        Case 2
            btnPrevious.Visible = True
            PieChart2Employers.Visible = True
            CreatePieData2Employers
        Case 3
            PieChartCashCredit.Visible = True
            CreatePieDataCashCredit
        Case 4
            LineChartAvgTips.Visible = True
            CreateLineChartAvgTips
        Case 5
            BarChartBestTipDays.Visible = True
            CreateBarChartBestTipDays
        Case 6
            StackedBarChartActualVsMin.Visible = True
            CreateStackedBarChartActualVsMin
        Case 7
            BarChartEarningsPerHour.Visible = True
            CreateBarChartEarningsPerHour
        Case 8
            PieChartTipRetention.Visible = True
            CreatePieChartTipRetention
        Case 9
            PieChartMileage.Visible = True
            CreatePieChartMileage
        Case 10
            BarChartBestTipHours.Visible = True
            CreateBarChartBestTipHours
        Case 11
            btnNext.Visible = True
            BarChartDeliveriesByHour.Visible = True
            CreateBarChartDeliveriesByHour
        Case 12
            btnPrevious.Visible = True
            btnNext.Visible = False
            BarChartDeliveriesByDay.Visible = True
            CreateBarChartDeliveriesByDay
    End Select
End Sub

Private Sub iad_Ready (Success As Boolean)
    If Main.UseAds Then
        If Success Then
            iad.Show(PageChartsAndGraphs) 'show ad
            '        iad.RequestAd 'request a new one!
        Else
            Log("ad not ready to show")
        End If
    End If
End Sub

Private Sub ResetLayout
    PageChartsAndGraphs.RootPanel.RemoveAllViews
    PageChartsAndGraphs.RootPanel.LoadLayout("MultiCharts")
    PageChartsAndGraphs.Title = Main.Localize.Localize("Charts and Graphs")  'List(PageTitle).Get(0)
    Main.Localize.LocalizeLayout(PageChartsAndGraphs.RootPanel)
End Sub
 
Upvote 0

Dennis Glowack

Member
Licensed User
Project is too large to attach. I emailed it to you. I suspect that whatever is going on here is also related to another issue. If you select Charts and Graphs from the main menu, leave page, then select Alerts, the table view does not fill the screen. If I leave the partially displayed Alerts screen, then reselect it, the entire table is displayed. The screen is not being re-initialized.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Clearly, I don't understand something about how pages are managed.

When you re-initialize a page, previous Page (UIViewController) will be removed.
But variables, declared in Process_Globals, do not have any relation to UIViewController instance.

To understand how b4i works, imagine following code
B4X:
Sub Something
      Dim i  As Int
      Dim Page As Object
      ...
End Sub

Page re-initializing is equal to assignment a new value to Page variable. Meanwhile i will not be changed.
So, you need to reset global variables 'manually'.
 
Last edited:
Upvote 0

Dennis Glowack

Member
Licensed User
Ok. Got a small project. 2 menu options.
Attached
When you re-initialize a page, previous Page (UIViewController) will be removed.
But variables, declared in Process_Globals, do not have any relation to UIViewController instance.

To understand how b4i works, imagine following code
B4X:
Sub Something
      Dim i  As Int
      Dim Page As Object
      ...
End Sub

Page re-initializing is equal to assignment a new value to Page variable. Meanwhile i will not be changed.
So, you need to reset global variables 'manually'.

That is consistent with what I have experienced. Now, I understand. Thanks.
 

Attachments

  • PartialProject.zip
    433.1 KB · Views: 127
Upvote 0
Top