Transmitting data between activities, general approach

Penko

Active Member
Licensed User
Longtime User
Hello!

This thread is about a general B4A programming advice rather than a bug or a problem.

It is related to the interaction and data transmission between Activities.

More specifically, Activity AddEdit is the activity which edits the main data. From there, I want to call the Activity "FriendSelector".

"Friendselector" is an activity which lists all my friends as panels and when I click a certain panel, the corresponding friend is selected.

My question is, if the code I've written is the best way to transmit data between two activities flawlessly. Bare in mind that this is a dual process: Activity "AddEdit" calls "FriendSelector" and then "FriendSelector" calls "AddEdit" again.

The more experienced than me, please let me know if there might be any issues with my solution. I have implemented this and it works perfectly. I just want to make sure there is no better way to do it.


AddEdit - edit user data, here a friend is a field which has to be selected

B4X:
Sub btnChooseFriend_Click

   CallSubDelayed2(FriendSelectorActivity, "selector", "AddEditActivity")

End Sub

Sub getBack(data As tFriend) ' this is called from the Selector Activity when it is done with the choice(the friend is selected).
   
   Msgbox(data.FriendName, "Your friend name")
   
   myFriendData = data ' a global variable and/or a view here to store the information
   
End Sub



Friend selector - select a friend and get back to the previous Activity.
B4X:
Sub selector(fromPlace As String) ' this is called with CallSubDelayed from the previous activity.

   came_from = fromPlace ' this global variable stores the Activity we should get back to after selecting the content.

End Sub

Sub selectorGetBack(friend As tFriend

   If(SubExists(came_from, "getBack")) Then
      CallSubDelayed2(came_from, "getBack", friend) 
   End If

End Sub

Sub pnll_Click

Dim thePanel As Panel : thePanel = Sender

Dim theData As tFriend : theData = thePanel.Tag

If(came_from <> "") Then
   selectorGetBack(theData)
   came_from = ""
   Activity.Finish
End If

End Sub

To summarize, I want to develop a ContentPicker with CallSubDelayed. I am not very acquintained with StartForResult, that's why I went for this approach.

Let me know what you think and if this is the correct way.
 
Top