Android Question Reusing UI Code Between Activities

DawningTruth

Active Member
Licensed User
I have created a custom modal box using B4XDialog. It is saved in its own Sub in one of my activities.

I want to use this sub for many different activities as my standard Modal Dialog for my app.

I have tired copying the code into both a class module and a code module, but neither seem to work with UI/Activity code.

Any suggestions on how to accomplish this aim?
 

DawningTruth

Active Member
Licensed User
It should be implemented in a class. You can then create a class instance in each of your activities.

Thx, tried the suggested approach seems to be working except for the following crash:

Error occurred on line: 51 (UiElements)
java.lang.RuntimeException: Class instance was not initialized (b4xdialog)
at anywheresoftware.b4a.debug.Debug.shouldDelegate(Debug.java:242)
at b4a.example.b4xdialog._showcustom(b4xdialog.java:196)
at b4a.example.uielements$ResumableSub_MessageBox.resume(uielements.java:171)
at b4a.example.uielements._messagebox(uielements.java:54)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

** Service (starter) Destroy (ignored)**

B4X:
// Calling Actitivty

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.   
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
     
    //...
    'UI Elements
    Private uiElements_ As UiElements
    //...

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:

    //...
   'Initialise
   uiElements_.Initialize
    //...
   
End Sub

    //...
// Calling Code

Sub CallMessageBox

    //...
    Wait For ( _
                   uiElements_.MessageBox("Note Saved", message,"","Ok","",200dip,xui_.Color_ARGB(255,76,57,165),xui_.Color_ARGB(255,255,140,0),xui_.Color_ARGB(255,76,57,165))) _
                   Complete (userResponse As Int)
    //...

End Sub

B4X:
//Called Class - UiElements

Sub Class_Globals
    
    'Standard Message Box
    Private dialog As B4XDialog
    Private myXui As XUI
    Private lbl_DialogTitle As B4XView
    Private lbl_DialogText As B4XView
    
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    
End Sub

Public Sub MessageBox(title As String, message As String, positive As String, cancel As String, negative As String, height As Int, colorPositive As Int, colorCancel As Int, colorNegative As Int) As ResumableSub
    
    //Message Box code here...
       
End Sub
 
Upvote 0

DawningTruth

Active Member
Licensed User
Found the error. Basically the dialog was not bound to anything as it is in a separate class, hence the crash. So, in the calling function had to initialize the dialog and then pass it into the function in the class.

B4X:
//Calling Function
dialog.Initialize(Activity)

Wait For ( _
                    uiElements_.MessageBox("Note Saved", message,"","Ok","",200dip,xui_.Color_ARGB(255,76,57,165),xui_.Color_ARGB(255,255,140,0),xui_.Color_ARGB(255,76,57,165),dialog)) _
                     Complete (userResponse As Int)
 
Upvote 0
Top