Android Question Passing several value to another activity

fasilosman

Active Member
Licensed User
Longtime User
HI,

I don't the exact command to pass more than one values/parameters to another activity.

I use "CallSubDelayed2" two times to pass 2 values.

But the targeted activity is created two times.

Can any body advice me in this regard.

Thank you
 

DonManfred

Expert
Licensed User
Longtime User
Use a MAP to store all Values you need to Transport.
Use this map in the callsubdelayed2 call as value
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
No one likes the classes.
I understand that in some cases it's easier to use a Map (I love the Maps) or an array, but... there is little talk of classes, in this site.
Yet, "severl values", often means a precise object.

(A little bit off topic, I know; but at least I'll add a post in which you talk about classes :D)
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
No one likes the classes.
I understand that in some cases it's easier to use a Map (I love the Maps) or an array, but... there is little talk of classes, in this site.
Yet, "severl values", often means a precise object.

(A little bit off topic, I know; but at least I'll add a post in which you talk about classes :D)

Interesting, that would be good for many of my applications.

Is a class passed by reference?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Is a class passed by reference?
you cannot pass a reference to an class-instance to another activity. Each activity can - or need to use - an own instance of the class.

Classes are not a good idea here to pass values from activity A to B
 
Upvote 1

SteveTerrell

Active Member
Licensed User
Longtime User
you cannot pass a reference to an class-instance to another activity. Each activity can - or need to use - an own instance of the class.

Classes are not a good idea here to pass values from activity A to B

Looks possible in a single activity though where different instances of the same class can be passed as parameters to a sub. From a quick timing check it looks like it is by reference.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I see no logical things, here.

I'm attaching a test project.

In Test 1 I pass an object of type AHLocale from Main to SecondActivity through a CallSubDelayed2; I get this error:
java.lang.Exception: Sub setlocale signature does not match expected signature.


In Test 2 I try to pass an object created from a my class and I get a different error:
java.lang.ClassCastException: java.lang.Object[] cannot be cast to b4a.example.myclass



However, this is an absurd limitation, never seen in other languages.

Classes in B4A, at this point, do not work as they should.
 

Attachments

  • Classes and Activities.zip
    14.4 KB · Views: 195
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I think that all languages treat arrays as arrays and non-arrays as non-arrays.

Your code is wrong. You shouldn't pass an array if the sub expects a non-array parameter.

Yes, that's was the error in Test 1, but my comments about "other languages" concerns classes/objects.
In fact, I do not get errors now from test 1, passing an object to the routine of SecondActivity (Local object), but I can not pass an object created from my class.


B4X:
Public Sub SetMyClassObject(MyClassObject As Object) ' <--- this does not work
'Public Sub SetMyClassObject(MyClassObject As MyClass)  ' <--- this does not work
    Dim MyObject As MyClass = MyClassObject
    Log(MyObject.Name)
End Sub

Public Sub SetLocale(Loc As AHLocale) ' <---  this works, Loc is an object
    Log(Loc.Country)
End Sub
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Works here fine after I fixed your code and removed the array.

I had already fixed the error (array) in the project attached in #12.

But neither of these declarations work:
B4X:
Public Sub SetMyClassObject(MyClassObject As Object) ' <--- this does not work
'Public Sub SetMyClassObject(MyClassObject As MyClass)  ' <--- this does not work
    Dim MyObject As MyClass = MyClassObject
    Log(MyObject.Name)
End Sub
 
Upvote 0
Top