Android Question Pass data from the main activity to the new activity

Wkm01pro

Member
Hello everyone, I state that I am new to the forum but above all new to B4A. I need to start with the basics and so I wanted to ask you how to pass data from the main activity (main) to a new activity module (for example: "activity1"). For data I mean for example a string "Hello", and that in the new activity a label (editText or whatever) takes this string inside and writes it to the screen. I don't know if I have made myself clear? If this is not possible, make sure that clicking a button automatically compiles the String on the screen inside the Label or EditText.

On the forum I found something similar but too complicated stuff (how to write and read a text file for example) right now I would like the simplest example possible.

Thank you so much to all of you.

ps. where did you study B4A?
 

JohnC

Expert
Licensed User
Longtime User
Just declare a variable in the "Process_Globals" section to hold the value you want to pass between activities:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim ValueToPass as String
    
End Sub
And then simply access that variable from the new activity.
 
Upvote 0

Wkm01pro

Member
Thanks for the reply, but.... I write "Dim ValueToPass As String = 'Hello'" on main acitivity. Then on the second activity to call it and write the data I do this:
"Label1.Text = Main.ValueToPass"? and does it write it to me on the screen? Sorry for my ignorance :)
-Wkm01pro
 
Upvote 0

Wkm01pro

Member
Thanks to all, I tried on the predefined activities and it doesn't work, I tried to do it through B4XPages and it works ... the code is the same I don't know why ... anyway thanks, I'm starting to understand a little bit.
 
Upvote 0

Wkm01pro

Member
Here it is: I state that the same code on B4XPages works. I'm definitely doing something wrong ? Thanks for your times.
 

Attachments

  • Datibase.zip
    9.6 KB · Views: 271
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are.
The problem is that you don't start the second Activity, frmModule1.
Attached a modified version.
With a Button on Activity Main which starts the frmModule1 Activity.

You may also have a look at this thread Different examples with 2 layouts.
It is a bit old, but it shows different methods to manage several layouts. It was written long before B4XPages did exist.

But, anyway, using B4XPages is the solution.
 

Attachments

  • Datibase1.zip
    10.5 KB · Views: 280
Last edited:
Upvote 0

Wkm01pro

Member
Ah ok I understand now! I really like this world, I will definitely ask another questions being a beginner, thanks !! For work we all have projects on Default and not B4XPages, but I try to study that too. ;)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
There is much simpler and better solution, use B4XPages.
But, anyway, using B4XPages is the solution.
1608125616759.png


https://www.b4x.com/android/forum/t...-activity-a-nuova-activity.125608/post-784503
 
Upvote 0

Albert Kallal

Active Member
Licensed User
Ah ok I understand now! I really like this world, I will definitely ask another questions being a beginner, thanks !! For work we all have projects on Default and not B4XPages, but I try to study that too. ;)

Ok, me having come from a strong Access and VB desktop background?

Well, first, I never have given credit to one REALLY silly and simple feature of B4A? And that feature is this:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Do you all see it? it is so simple, so not noticed. That is the blank code template with those BEYOND simple and helpful comments!!!

I jump around so many platforms in a day - it is HARD to remember. So, I cannot state how many late nights - and those simple, bare bones, limited, but most welcome comments? They helped me - a LOT!! They shortened the learning curve of B4A.

I kind of wish more platforms did this. Just thought I would give a kind shout out to something that we don't really notice - but those default comments really do deserve a group hug.

So, often to pass values? Well I don't have say parameters to the next "form" to open.

So, I tend to think of launch new form (next form?). That is this:

B4X:
Sub btnForm1_Click
    Form2.strMyPassedTextValue = "my value to pass"
    StartActivity(Form2)
End Sub

So above will launch my Form2. Now not all forms (layout) need their own activity. But the idea is quite much:
Add the activity, then add the form. They are often paired up this way. And the trick of when to create a activty or not?
Well, that just a learning curve.

And if you hit the back key on your phone, the form will close and we return to previous.
or we can have a close button on that 2nd form such as:
B4X:
Sub btnBack_Click

    Activity.Finish
   
End Sub

And the code to shove that "passed" value into a text box? we have this
in our so called "load event"

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Form1")
   
    If FirstTime Then
        EditText1.Text = strMyPassedTextValue
    End If

End Sub
Now not in all cases do you need a "whole new" code module (activity) paired up with a layout, but I don't mind this setup.

One good aspect? It moves code into the given layout.

I starting to shove too much code into one spot - so fighting this urge somewhat!

And, in the sprit of the holidays? Well, I JUST minutes ago finally upgraded from B4A 9.0x to current.

(IDE: like the project close button! (really!!! - I always wanted that!!!)
(like the more zoom levels).
(like the use of space up top with the module tabs - nice!)

And I JUST did my first B4xpage. Its is nice.

I'll start absorbing what options to break code out to additonal modules - and when to do so.
(this is not 100% clear in my brain just quite yet).

Reduction of having to deal (fight) with Activity? yes - that is welcome.

So, it not more then really 10 minutes ago - my first try at this!

I can say that having grasped how B4A works? Then the jump to B4A pages is MUCH less of a curve then I expected.
Really - might have missed something - but I was expecting more hardship! - there was little!!

And to be fair? Well, I do much respect how B4A does continue and allow me to work with the benefits of activities, and how VERY cool that architecture of Android is in this regards.

Very busy - but at least I have the new B4A up and running. The emulator not working - spitting out some errors - but don't use it much - and am busy - it was try and fix that or write this post!!).

Regards,
Albert D. Kallal
Edmonton, Alberta Canada
 
Upvote 0
Top