Android Question Combining strings from multiple activities

gareththomasnz

Member
Licensed User
Longtime User
This is a continuation of my previous project

https://www.b4x.com/android/forum/t...y-using-input-from-multiple-activities.56724/

Same app - nearly finished

Now the client wants to take a photo of the person filling the forms and store it in the database also - or maybe just the filepath to the photo

I want to name the photo with the firstname middlename & lastname

So far I can get this to work only from the main activity and only when I press the back button as the middlename & last name are on the next activity

The photo activity is after all of the forms are filled out

I assumed I could pass the text data to the photo activity just as I pass it to main - but it doesn't work

I cant use subdelayed as the app does not return to main after taking the photo

Why is the data not being passed to the photo activity?

How can I create the string for use in the photo activity?

On main I have this code:

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    Log(txtFirstName.Text & MiddleName & LastName &".jpg")

    If UserClosed Then
        SQL1.Close        'if the user closes the program we close the database
    End If
End Sub

txt.FirstName is entered on the Mains associated GUI interface

MiddleName & LastName are passed from the next activity

This creates the desired petersimonsmith.jpg in the log

The following lines in activity two pass the text values into activity main

B4X:
Sub Activity_Pause (UserClosed As Boolean)

    Main.MiddleName      = txtMidName.Text
    Main.LastName        = txtLastName.Text

That all works fine & its also the method I use for creating the database query

OK so why does the exact same technique not work at all on the final photo panel?


I tried putting
B4X:
 PanelPhoto.MiddleName = txtMidName.Text

etc on panel two

and
B4X:
Log(FirstName & MiddleName & LastName &".jpg")

on PanelPhoto but all it shows is .jpg

What is wrong?
 

gareththomasnz

Member
Licensed User
Longtime User
I am thinking now - I could simply save the photo as 1.jpg

But rename it with the "firstname middlename lastname" when I run the database save query using callsubdelayed

https://www.b4x.com/android/forum/threads/rename-a-file-in-assets.39346/

This while convoluted seems the easiest option

B4X:
Sub CreateSaveFileName
    SaveFileName = txtFirstName.Text & MiddleName & LastName &".jpg"
End Sub




Sub Activity_Resume
    If SQL1.IsInitialized = False Then
        SQL1.Initialize(File.DirInternal, "electapp.db", True)
    End If

    CreateSaveFileName

End Sub

This should create the file name upon resume which is before callsubdeleayed calls the database add query

So I can also add the file path or the file itself to the database - but before this I must actually rename the file which I will do in another sub

the file rename is something like this
B4X:
Dim Success as boolean
Success=RenameFile(File.DirRootExternal & "/path/oldfilename.txt", File.DirRootExternal & "/path/newfilename.txt")

Sub RenameFile(OriginalFileName As String, NewFileName As String) As Boolean
    Dim Result As Int
    Dim StdOut, StdErr As StringBuilder
    StdOut.Initialize
    StdErr.Initialize
    Dim Ph As Phone
    Result = Ph.Shell("mv " & OriginalFileName & " " & NewFileName, Null,  StdOut, StdErr)
    If Result = 0 Then
        Return True
    Else
        Return False
    End If
End Sub

And there are other ways https://www.b4x.com/android/forum/threads/how-do-you-rename-a-file.19215/

so the order for calling the subs should be

CreateSaveFileName

RenameFile

AddEntry

Doing this also allows the user to retake the photo & overwrite 1.jpg without any complications before it gets entered in the database
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Sorry, I do not have the patience to read it all well ;)

In general, to pass values between Activities, you have at least three options:

a) write them in a file (KeyValueStore can be a solution);
b) declare a variable in the Process_Globals of an Activity and set its value from wherever you want;
c) use CallSubDelayed2 (or CallSubDelayed3).
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Sorry, I do not have the patience to read it all well ;)

In general, to pass values between Activities, you have at least three options:

a) write them in a file (KeyValueStore can be a solution);
b) declare a variable in the Process_Globals of an Activity and set its value from wherever you want;
c) use CallSubDelayed2 (or CallSubDelayed3).
@gareththomasnz if I have understood your requirement correctly then I believe that option b posted by LucaMS will be the best solution for you. By saving the values to a process global you will be able to access them anywhere and you can also clear the values as and when you want.
 
Upvote 0
Top