Android Question Passing variable/parameter to an Activity

Anser

Well-Known Member
Licensed User
Longtime User
Hi Friends,

Is there any way to pass a variable/parameter to an Activity, while calling the StartActivity()

For Eg
StartActivity("ActivityName", MyVariableValue)

Instead of creating several activities, if a parameter can be passed and then inside the Activity, based on the value of the received in the parameter, I can change the caption/text, Button_Click actions etc..

I know that I can use Process Global variables, which can be accessed from all activities, but I hate playing with the Public/Global variables unnecessarily. If there is a chance, I would avoid using Public/Global variables because the chances of logical mistakes creeping in are high and on a later date debugging the app may become a nightmare.

Any hint that points me to the right direction will be appreciated.

Regards
Anser
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
Unfortunately it is not working as desired by me. I checked the value of the Parameter and the value is received there, but I am not able to change the Text on the button based on the Parameter's value

I referred this post too.
https://www.b4x.com/android/forum/threads/callsubdelayed2-for-multi-activity.54381/#post-341530

I am sure that its something wrong the way I am doing it.

From Activity One
B4X:
Sub BtnView_Click
    'StartActivity("EstimateList")   
    CallSubDelayed2(EstimateList,"StartWithParameter", "View")
End Sub

Sub BtnEdit_Click
    'StartActivity("EstimateList")       
    CallSubDelayed2(EstimateList,"StartWithParameter", "Edit")
End Sub

In the Activity named EstimateList
B4X:
Sub Globals

    Private Btn As ACFlatButton
    Private cCaledFrom As String

End Sub

Sub StartWithParameter(CalledFrom As String)
    cCaledFrom = CalledFrom
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("EstimateList")

    'The Text on the Button is not updated as per the following lines
    If cCaledFrom = "View" Then
        Btn.Text = "View Estimate Details"
    Else If cCaledFrom = "Edit" Then
        Btn.Text = "Edit Estimate's contents"
    End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Btn_Click
    ' Here the MsgBox shows the correct Value of the variable
    Msgbox(cCaledFrom,"Parameter Value")       
End Sub

I tried changing the Btn's Text in Activity_Resume. Even then it is not changing, but if I put the display OFF on my phone using the power button and then switch the display ON then the Btn's text is updated. I can't understand what is wrong.

Regards
Anser
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
Have you tried to define the variable cCaledFrom in Process_Globals and then:
B4X:
Sub BtnView_Click
    EstimateList.cCaledFrom = "View"
    StartActivity("EstimateList")  
End Sub

Sub BtnEdit_Click
    EstimateList.cCaledFrom = "Edit"
    StartActivity("EstimateList")      
End Sub
 
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
Have you tried to define the variable cCaledFrom in Process_Globals and then:

I am already using this Process_Global solution. My intention is to avoid or reduce dependency on Process Global variables.

I know that I can use Process Global variables, which can be accessed from all activities, but I hate playing with the Public/Global variables unnecessarily. If there is a chance, I would avoid using Public/Global variables because the chances of logical mistakes creeping in are high and on a later date debugging the app may become a nightmare.
Regards

Anser



Or you can create an Intent to start a new Activity and set 'extra' values in the Intent.
Take a look here: https://www.b4x.com/android/forum/t...app-communication-with-intents.30608/#content

Let me read this and understand the concept.
If it is too complicated then I will rely on the Process_Global variable itself.

Still can't understand why the Button text is not updated, but when I check the parameter value from the Sub Button_Click, the value in the parameter is received correctly in the activity.

Regards
Anser
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
Still can't understand why the Button text is not updated, but when I check the parameter value from the Sub Button_Click, the value in the parameter is received correctly in the activity.

Read carefully: https://www.b4x.com/android/forum/t...teract-between-activities-and-services.18691/

  • If the target module is not active, then it will be started automatically. When the target module is ready, the sub will be called.
  • CallSubDelayed doesn't immediately call the target sub. It sends a message to the message queue. The internal framework manages this message and passes it to the target module when it is ready.
  • The sub will be called before Activity_Resume.

So your target sub is probably executed after Activity_Create but before Activity_Resume.
Try this:

B4X:
Sub StartWithParameter(CalledFrom As String)
    cCaledFrom = CalledFrom
    If cCaledFrom = "View" Then
        Btn.Text = "View Estimate Details"
    Else If cCaledFrom = "Edit" Then
        Btn.Text = "Edit Estimate's contents"
    End If
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("EstimateList")
End Sub
 
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
Thanks warwound. :)

Your suggestion worked.

I moved the code to change the Button's text from Activity_Create to Sub StartWithParameter. Now, its working as expected.

Regards

Anser
 
Upvote 0
Top