Pass Class instance to Activity with Intent

warwound

Expert
Licensed User
Longtime User
Hi.

Can i pass an instance of a class to another activity using an intent?

Here's my DirectionsLeg class:

B4X:
'   Class module represents a DirectionsResult.DirectionsRoute.DirectionsLeg
'   https://developers.google.com/maps/documentation/javascript/reference#DirectionsResult
Sub Class_Globals

   '   to do: check that defining a Type in a Class is ok

   Type DirectionsStepType(Distance As DistanceType, Duration As DurationType, EndLocation As LatLngType, Instructions As String, _
      Path() As LatLngType, StartLocation As LatLngType, TravelMode As String)
   Type DistanceType(Text As String, Value As Int)
   Type DurationType(Text As String, Value As Int)
   Type LatLngType(Latitude As Double, Longitude As Double)
   
   Public Distance As DistanceType
   Public Duration As DurationType
   Public EndAddress As String
   Public EndLocation As LatLngType
   Public StartAddress As String
   Public StartLocation As LatLngType
   Public Steps() As DirectionsStepType
   
End Sub

'   Initializes the DirectionsLeg object. Parsing the JSONText to class properties.
Public Sub Initialize(JSONText As String)
   Dim TempList As List
   Dim TempMap, TempMap2 As Map
   
   Dim Parser As JSONParser
   Parser.Initialize(JSONText)
   
   Dim JSONObject As Map
   JSONObject=Parser.NextObject
   
   TempMap=JSONObject.Get("distance")
   Distance.Initialize
   Distance.Text=TempMap.Get("text")
   Distance.Value=TempMap.Get("value")
   
   TempMap=JSONObject.Get("duration")
   Duration.Initialize
   Duration.Text=TempMap.Get("text")
   Duration.Value=TempMap.Get("value")
   
   EndAddress=JSONObject.Get("end_address")
   
   TempMap=JSONObject.Get("end_location")
   EndLocation.Initialize
   EndLocation.Latitude=TempMap.GetValueAt(0)
   EndLocation.Longitude=TempMap.GetValueAt(1)
   
   StartAddress=JSONObject.Get("start_address")
   
   TempMap=JSONObject.Get("start_location")
   StartLocation.Initialize
   StartLocation.Latitude=TempMap.GetValueAt(0)
   StartLocation.Longitude=TempMap.GetValueAt(1)
   
   TempList=JSONObject.Get("steps")   '   TempList is now an Array of javascript DirectionStep objects
   Dim i, StepsCount As Int
   StepsCount=TempList.Size
   Dim Steps(StepsCount) As DirectionsStepType
   For i=0 To StepsCount-1
      Steps(i).Initialize   '   after initializing Steps(i) do we still need to initialize the child properties such as Distance?
      
      TempMap=TempList.Get(i)   '   TempMap is now a javascript DirectionsStep object
      TempMap2=TempMap.Get("distance")
      '   Steps(i).Distance.Initialize
      Steps(i).Distance.Text=TempMap2.Get("text")
      Steps(i).Distance.Value=TempMap2.Get("value")
      
      TempMap2=TempMap.Get("duration")
      '   Steps(i).Duration.Initialize
      Steps(i).Duration.Text=TempMap2.Get("text")
      Steps(i).Duration.Value=TempMap2.Get("value")
      
      TempMap2=TempMap.Get("end_location")
      '   Steps(i).EndLocation.Initialize
      Steps(i).EndLocation.Latitude=TempMap2.GetValueAt(0)
      Steps(i).EndLocation.Longitude=TempMap2.GetValueAt(1)
      
      Steps(i).Instructions=TempMap.Get("instructions")
      
      '   Steps(i).Path   '   not parsed as it may not be required, it's an Array of LatLng coords of this part of the route
      
      TempMap2=TempMap.Get("start_location")
      '   Steps(i).EndLocation.Initialize
      Steps(i).StartLocation.Latitude=TempMap2.GetValueAt(0)
      Steps(i).StartLocation.Longitude=TempMap2.GetValueAt(1)
      
      Steps(i).TravelMode=TempMap.Get("travel_mode")
   Next
   
End Sub

In activity#1 i do this:

B4X:
Sub WebView_SaveDirections(JSONText As String)
   Dim DirectionsLeg1 As DirectionsLeg
   DirectionsLeg1.Initialize(JSONText)
   
   Dim Intent1 As Intent
   Intent1.Initialize(Intent1.ACTION_MAIN, "")
   Intent1.SetComponent("package.name.here/.offlinedirections")
   Intent1.PutExtra("DirectionsLeg", DirectionsLeg1)
   '   Intent1.PutExtra("JSONText", JSONText)
   StartActivity(Intent1)
   
End Sub

And my OfflineDirections activity:

B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ListView1.Initialize("")
   Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
   
   Dim Intent1 As Intent
   Intent1=Activity.GetStartingIntent
   If Intent1.HasExtra("DirectionsLeg") Then

      Log("debug")
      
      Dim DirectionsLeg1 As DirectionsLeg
      DirectionsLeg1=Intent1.getExtra("DirectionsLeg")
      
      ListView1.AddTwoLines("From:", DirectionsLeg1.StartAddress)
      
      ListView1.AddTwoLines("To:", DirectionsLeg1.EndAddress)
   End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

The instance of my DirectionsLeg class is never present in the OfflineDirections activity StartingIntent.

If i uncomment the line:

B4X:
   '   Intent1.PutExtra("JSONText", JSONText)

The StartingActivity HasExtra("JSONText") returns True.

Any ideas?

Martin.
 

warwound

Expert
Licensed User
Longtime User
Ok thanks.

In fact for this project i can pass my JSON String using an Intent and create the class instance in the new Activity using that String.

But for future reference i now know a class instance cannot be used as an Intent extra!

Martin.
 
Upvote 0
Top