Android Question Run Sub in B4XPage from class and wait for it to be done

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Need to get the Root and Class instance (Me) of a B4XPage in a normal class and make sure it is all done before I continue.

Tried this one:

in the B4XPage:

B4X:
Sub PassClassAndRoot
    
    Log("PassClassAndRoot")
    cMenu.Root = Root
    cMenu.B4XPage = Me
    CallSubDelayed(cMenu, "PassClassAndRoot_Done")
    
End Sub

In the class:

B4X:
Sub setRoot(oRoot As B4XView)
    Log("setRoot, oRoot.Tag: " & oRoot.Tag)
    moRoot = oRoot
End Sub

Sub setB4XPage(oB4XPage As Object)
    mCallBack = oB4XPage
End Sub

calling it in the class like this from a normal Sub:

B4X:
CallSubDelayed(B4XPages.GetPage("Page" & miButtonStartIndex), "PassClassAndRoot")
Wait For PassClassAndRoot_Done Complete

It runs all fine, but it doesn't wait, so I get the wrong B4XPage root (the old one) to work with.

Any suggestions how this should be done?

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Need to get the Root and Class instance (Me) of a B4XPage in a normal class and make sure it is all done before I continue.

Tried this one:

in the B4XPage:

B4X:
Sub PassClassAndRoot
   
    Log("PassClassAndRoot")
    cMenu.Root = Root
    cMenu.B4XPage = Me
    CallSubDelayed(cMenu, "PassClassAndRoot_Done")
   
End Sub

In the class:

B4X:
Sub setRoot(oRoot As B4XView)
    Log("setRoot, oRoot.Tag: " & oRoot.Tag)
    moRoot = oRoot
End Sub

Sub setB4XPage(oB4XPage As Object)
    mCallBack = oB4XPage
End Sub

calling it in the class like this from a normal Sub:

B4X:
CallSubDelayed(B4XPages.GetPage("Page" & miButtonStartIndex), "PassClassAndRoot")
Wait For PassClassAndRoot_Done Complete

It runs all fine, but it doesn't wait, so I get the wrong B4XPage root (the old one) to work with.

Any suggestions how this should be done?

RBS
This seems to work OK:

In the B4XPage:

B4X:
Sub Get_PageClass() As Object
    Return Me
End Sub

Sub Get_PageRoot() As B4XView
    Return Root
End Sub

In the normal class:

B4X:
Sub GetRootAndClass(strPageID As String) As ResumableSub
    
    Log("GetRootAndClass, strPageID: " & strPageID)
    
    moRoot = CallSub(B4XPages.GetPage(strPageID), "Get_PageRoot")
    mCallBack = CallSub(B4XPages.GetPage(strPageID), "Get_PageClass")
    
    Return True
    
End Sub

And called in this class like this:

B4X:
Dim rs As ResumableSub =  GetRootAndClass("Page" & miButtonStartIndex)
Wait For (rs) Complete (bDone As Boolean)

Although my code logs show I get the right page root (from the Tag of the root), I still have problems (layout not showing) and not sure the layout was added to the right page, looking at my log and B4XPages.GetManager.LogEvents (set to True):

SetMenuBar, miButtonStartIndex: 8, iMenuButtonEndIndex: 14
SetMenuBar, iHighlightedButton: -1
*** page0: B4XPage_Disappear [mainpage, page0]
*** page8: B4XPage_Appear [mainpage, page0] <<<<<<<<< I expected this to be page8
GetRootAndClass, strPageID: Page8
SetMenuBar, bDone: true
SetMenuBar, moRoot.Tag: 8
>>> B4XPage_Appear: page8 appeared

I think this might need another question as it is a different problem.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This seems to work OK:

In the B4XPage:

B4X:
Sub Get_PageClass() As Object
    Return Me
End Sub

Sub Get_PageRoot() As B4XView
    Return Root
End Sub

In the normal class:

B4X:
Sub GetRootAndClass(strPageID As String) As ResumableSub
   
    Log("GetRootAndClass, strPageID: " & strPageID)
   
    moRoot = CallSub(B4XPages.GetPage(strPageID), "Get_PageRoot")
    mCallBack = CallSub(B4XPages.GetPage(strPageID), "Get_PageClass")
   
    Return True
   
End Sub

And called in this class like this:

B4X:
Dim rs As ResumableSub =  GetRootAndClass("Page" & miButtonStartIndex)
Wait For (rs) Complete (bDone As Boolean)

Although my code logs show I get the right page root (from the Tag of the root), I still have problems (layout not showing) and not sure the layout was added to the right page, looking at my log and B4XPages.GetManager.LogEvents (set to True):

SetMenuBar, miButtonStartIndex: 8, iMenuButtonEndIndex: 14
SetMenuBar, iHighlightedButton: -1
*** page0: B4XPage_Disappear [mainpage, page0]
*** page8: B4XPage_Appear [mainpage, page0] <<<<<<<<< I expected this to be page8
GetRootAndClass, strPageID: Page8
SetMenuBar, bDone: true
SetMenuBar, moRoot.Tag: 8
>>> B4XPage_Appear: page8 appeared

I think this might need another question as it is a different problem.

RBS
> I think this might need another question as it is a different problem.

Ignore this, problem was simply that the panel holding the views was not visible. This was the default in my old non-B4XPages app.

RBS
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
You need a B4XPage to call a Method from a Class and the B4XPage must wait until the Class completed it's job?
 

Attachments

  • Page_Wait.zip
    14.6 KB · Views: 106
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
You need a B4XPage to call a Method from a Class and the B4XPage must wait until the Class completed it's job?
I am aware of CallSubDelayed and Wait For (the event). I coded this in a different way (ResumableSub) and all working fine.
Thank in any case.
Got this menu class nearly done now and will post it when it is presentable.

RBS
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
mmm... I have insufficient insight into what exactly the intended program should do.
B4XPages is a class that captures a snapshot in an object and then makes adjustments to that object. If you have a situation where you make certain adjustments in a class and then leave that class alone and the program continues in another class, then you need to be very aware that in other environments there may be things that change the class with know nothing about an old snapshot.
That is why it is sometimes better to work from a B4XPages with a Global variable that is defined in the B4XMainPage and can be read and updated from all B4XPages so that all B4XPages can have the latest value. Especially if you have a B4Page with the option to leave it abruptly with the "blunt ax method", the question is whether the update(s) you provide will always be executed and can sometimes lead to errors that are difficult to trace that occur by chance.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
mmm... I have insufficient insight into what exactly the intended program should do.
B4XPages is a class that captures a snapshot in an object and then makes adjustments to that object. If you have a situation where you make certain adjustments in a class and then leave that class alone and the program continues in another class, then you need to be very aware that in other environments there may be things that change the class with know nothing about an old snapshot.
That is why it is sometimes better to work from a B4XPages with a Global variable that is defined in the B4XMainPage and can be read and updated from all B4XPages so that all B4XPages can have the latest value. Especially if you have a B4Page with the option to leave it abruptly with the "blunt ax method", the question is whether the update(s) you provide will always be executed and can sometimes lead to errors that are difficult to trace that occur by chance.
I decided a few days ago that it would be better to have only one instance of my menu class, rather that one for every page. This instance runs in B4XMainPage.

RBS
 
Upvote 0
Top