Android Question How to allow an Activity to be used from many different points in an app?

JohnC

Expert
Licensed User
Longtime User
I have an activity called "SendSMS" that provides a very useful function to send an SMS message and I would like the ability to use this activity from many different points in my app.

For example, I would like to display this SendSMS activity from a sub in Activity "Main":

B4X:
Sub OK_Click
 
    Msgbox2Async("Do you want to send the customer an SMS message before Navigating to their address?", "Send SMS", "Yes", "Cancel", "No", Null, False)
    Wait For Msgbox_Result (Result As Int)
    If Result = DialogResponse.POSITIVE Then
        StartActivity(SendSMS)
    End If
 
    Dim Intent1 As Intent
    Dim URI As String
    URI = "google.navigation:q=" & Addr
    Intent1.Initialize(Intent1.ACTION_VIEW,URI)
    Intent1.SetComponent("google.navigation")
    StartActivity(Intent1)
 
End Sub

But the line "StartActivity(SendSMS)" is not blocking so it will both display the SendSMS and execution will immediately continue on and launch google maps at the same time - which is NOT good.

So I could break up the above routine into two parts:

B4X:
Sub OK_Click
 
    Msgbox2Async("Do you want to send the customer an SMS message before Navigating to their address?", "Send SMS", "Yes", "Cancel", "No", Null, False)
    Wait For Msgbox_Result (Result As Int)
    If Result = DialogResponse.POSITIVE Then
        StartActivity(SendSMS)
    End If

End Sub

Sub Nav
   
    Dim Intent1 As Intent
    Dim URI As String
    URI = "google.navigation:q=" & Addr
    Intent1.Initialize(Intent1.ACTION_VIEW,URI)
    Intent1.SetComponent("google.navigation")
    StartActivity(Intent1)
 
End Sub

And after the SMS is sent in the SendSMS activity, I could return control back the the Main activity by calling the second part using this:

B4X:
Sub SMSSent
 
    CallSubDelayed(Main, "Nav")
 
End Sub

But if I display the SendSMS activity from other parts of my app, I need to have a separate CallSubDelayed for each cooresponding spot, which will start making things messy.

I would rather like to have a true object-oriented solution so that I can display the SendSMS from any spot in my app without having to keep modifying the SendSMS activity.

It would be nice to have something like this:

B4X:
Wait For StartActivity(SendSMS)

Which would pause the routine while the activity is displayed, and resume the routine when the activity is closed.

Is there a way to do this, or some other solution that will allow me to display an activity from anywhere without having to keep track of what sub displayed the activity so I could resume execution of the routine that displayed the activity?
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
How would a code module work?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Create a Class with the needed Code
Create an instance of this class in each activity you want to use it.
 
Upvote 0
Top