Custom Activity_Create

Roger Garstang

Well-Known Member
Licensed User
Longtime User
How hard would it be to have Activity_Create work like Initialize in a class where you can customize it? Or even something like C++/C# and some other languages do with "static void Main(string[] args)" so we can pass values to an activity without needing Globals for everything.

To not break code we could even make a StartActivity2 (Even though all the numbered functions drive me nuts) that is able to pass an array called args that is just there and available like Sender is.
 

warwound

Expert
Licensed User
Longtime User
Or create an Intent and use it's PutExtra method to add your data to the Intent.

Then use the Intent to start the Activity and the started Activity can get the Intent that started it and use the Intent GetExtra method to retrieve that data you wanted to pass to it.

Activity #1:

B4X:
Dim Intent1 As Intent
Intent1.Initialize("", "")
Intent1.SetComponent("my.b4a.package.name/.the_activity_to_start_name")
Intent1.PutExtra("MyData", "Any object can be put as an extra")
StartActivity(Intent1)

Activity #2:

B4X:
Sub Activity_Create(FirstTime As Boolean)

   Dim StartingIntent As Intent
   StartingIntent=Activity.GetStartingIntent
   Dim Value As String
   Value=StartingIntent.GetExtra("MyData")
   
End Sub

Be sure to set the right value for Intent1.SetComponent().
If the Activity you want to start is named MyNewActivity you'd use:

B4X:
Intent1.SetComponent("my.b4a.package.name/.mynewactivity")

That is you need to use an entirely lower-case version of the Activity name.

Martin.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Cool. I like that Intent way. It is very close to the args way or environment variables.

I keep forgetting about the Delayed Sub starting the activity too. For some reason I think of that as starting the Activity, Executing the Sub, then exiting/pausing the Activity when in reality it keeps it open...and I thought it didn't call the sub until control reverted back to the Activity (Reading again now I guess that only applied to when it was in the background/a service). Either way could be useful depending on what needs done I guess.
 
Top