Android Question share a custom type between activities?

Branko Milosevic

Active Member
Licensed User
I have two custom types in two activities. They are exactly the same I had to declare one in each activity because it cannot be declared in process globals. I seem to have a problem saving a list of types in one activity and reading it into the list of identical but different types in the other activity. No compiler errors or warnings just crashes. Should the type be declared in a class module instead?
 

DonManfred

Expert
Licensed User
Longtime User
Post a small example project showing the problem.
A customType should be placed in process_globals and can be accessed from any activity in the prject.
The best place i guess is in the Start service.

Method_636.png
Type

Declares a structure.
Can only be used inside sub Globals or sub Process_Globals.
Syntax:
Type type-name (field1, field2, ...)
Fields include name and type.
Example:
Type MyType (Name As String, Items(10) As Int)
Dim a, b As MyType
a.Initialize
a.Items(2) = 123


Be inspired by this code...


Main
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private edtName As EditText
    Private edtStreet As EditText
    Private edtTown As EditText
    Private Button1 As Button
End Sub

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

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Button1_Click
    Starter.ActAddress.Name = edtName.Text
    Starter.ActAddress.Street = edtStreet.Text
    Starter.ActAddress.Town = edtTown.Text
    StartActivity(Act2)
End Sub

Starter
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Type address (Name As String, Street As String, Town As String)
    Dim ActAddress As address
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
End Sub

Sub Service_Start (StartingIntent As Intent)
    ActAddress.Initialize
   
End Sub

Act2
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private lblName As Label
    Private lblStreet As Label
    Private lblTown As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout2")
    lblName.Text = Starter.ActAddress.Name
    lblStreet.Text = Starter.ActAddress.Street
    lblTown.Text = Starter.ActAddress.Town
End Sub

Project attached
 

Attachments

  • ctEx.zip
    9.5 KB · Views: 146
Last edited:
Upvote 0

Branko Milosevic

Active Member
Licensed User
Thanks Don I for some reason thought it can't be declared inside process globals. No idea where that came from but I moved it to process globals and it's all fine.
 
Upvote 0
Top