How to use an Activity as a dialog

jkurant

Member
Licensed User
Longtime User
I have an app that is about Reminders. On my Main activity, I have a button that let's the user enter a new Reminder. I would use a CustomDialog, but that doesn't let me use the entire width of the screen.

So, I created another Activity called EditReminder. I can pass the ID of the database object that represents the Reminder in by using Process_Global variables. In my example, I am passing in a -1 to indicate I am adding a new Reminder.

So, I use this code:

B4X:
   EditReminder.EditedReminderID = -1
   StartActivity("EditReminder")
   ' Hoping I can access data from the EditReminder acivity now

After the activity is complete, I would hope to get the values back, but I don't know how to make this happen. Will the second activity return control to the code after StartActivity? How do I exit the EditReminder activity to make it return to right after it was called?

Here is some code from the EditReminder activity:
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Type Reminder(Description As String, When As String, Trigger As String, PlaceID As Int, DaysOfWeek As List, StartTimeWindowHours As Int, StartTimeWindowMinutes As Int, StopTimeWindowHours As Int, StopTimeWindowMinutes As Int)
   'When = { The next time, Any time  }
   'Trigger = { I Arrive at, I Depart from }
   'DaysOfWeek is a list of Booleans starting with Sunday as DaysOfWeek[0]
   'onSunday= d.Get(0) <> 0
   'onMonday= d.Get(1) <> 0
   'onTuesday= d.Get(2) <> 0
   'onWednesday= d.Get(3) <> 0
   'onThursday= d.Get(4) <> 0
   'onFriday= d.Get(5) <> 0
   'onSaturday= d.Get(6) <> 0

   Dim EditedReminder As Reminder
   Dim EditedReminderID As Int
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   ' Save the values from the view on the screen in a global object of type Reminder called EditedReminder
   Dim r As Reminder
   r.Initialize()
   r.Description = EditName.Text
   r.When = ""
   r.Trigger = SpinTrigger.SelectedItem
   r.PlaceID = GetPlaceIDFromSpinPlaces
   r.StartTimeWindowHours = EditStartHour.Text
   r.StartTimeWindowMinutes = EditStartMinute.Text
   r.StopTimeWindowHours = EditStopHour.Text
   r.StopTimeWindowMinutes = EditStopMinute.Text
   EditedReminder = r
   StartActivity("Main")
End Sub
 

Ricky D

Well-Known Member
Licensed User
Longtime User
you can't make it go back to where it was called because the activity gets paused when you open the edit activity.

Upon return Activity_Resume gets called.

I've done this in my app.

in
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim sOptions As String 
   Dim shiftOption As Int

then in code later

B4X:
   sOptions = "Show"
             'shiftoptions is my other activity we're going to show
'this line puts u.LongDateString(Record(1)) into the sSelectedShift which
'is in shiftoptions in it's Process Globals
   shiftoptions.sSelectedShift = u.LongDateString(Record(1))
   
   StartActivity(shiftoptions)

next put this kind of code into Activity_Resume

B4X:
   Dim m As Map
   
   If sOptions="Show" Then
      Select shiftOption
         Case 1   'Edit
            m = DBUtils.ExecuteMap(u.mySQL,"SELECT * FROM Shifts WHERE TableID=" & TableID,Null)
            
            u.Shift.Initialize
            u.Shift = u.ShiftDetails(m.Get("details"))

            
            u.Shift.AddingNew = False
            
            StartActivity("ShiftEdit")
            
         Case 2   'Set as Current
            'firstly clear any set to current
            u.SetAllIsCurrentOff 
            
            'now update the selected Shift in TableID
            Dim s As String
            s = "UPDATE Shifts SET IsCurrent=1 WHERE TableID=" & TableID
            u.mySQL.ExecNonQuery(s)
            Dim m As Map
            m.Initialize
            m = DBUtils.ExecuteMap(u.mySQL,"SELECT * FROM Shifts WHERE TableID=" & TableID,Null)
            u.ShiftDate = m.Get("shiftdate")
            
            u.Shift.Initialize 
            u.CurrentShift = u.ShiftDetails(m.Get("details"))
            
         Case 3   'Delete
            Dim result As Int
            Dim s As String
            
            result = Msgbox2("Are you sure you want to Delete Shift on " & u.LongDateString(Record(1)) & "?","Confirm Delete","Yes","","No",Null)
            
            If result=DialogResponse.POSITIVE Then
               s = "DELETE FROM Shifts Where TableID=" & TableID
               u.mySQL.ExecNonQuery(s)
               u.FixLastID("Shifts")
               LoadShifts
            End If   
      End Select
   End If
   
   sOptions = ""

and in my shiftoptions Activity here

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layoutshiftoptions")
   Activity.Color = Colors.Transparent 
   
   lSelectedShift.Text = "Shift " & sSelectedShift & " - Select option"
End Sub

and here is code to place the return value

B4X:
Sub bEdit_Click
   Shifts.shiftOption = 1
   Activity.Finish 
End Sub

Sub bSetAsCurrent_Click
   Shifts.shiftOption = 2
   Activity.Finish 
End Sub

Sub bDelete_Click
   Shifts.shiftOption = 3
   Activity.Finish 
End Sub

Sub bCancel_Click
   Shifts.shiftOption = -1
   Activity.Finish 
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   If KeyCode=KeyCodes.KEYCODE_BACK Then
      Shifts.shiftOption = -1      'Cancel
   End If
End Sub

you could return anything really

So....

1. Define Process_Globals variables in the calling activity

2. Define Process_Globals variables in the receiving activity

3. Return values back to calling activity via it's Process_Globals variable in 1.

4. Place code in Activity_Resume to receive the call back
You must set the switch to "" otherwise other things like the app being paused will cause this to run again with possible unpredictable results

5. Code to call the new activity

I hope this is clear enough.

regards, Ricky
 
Last edited:
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
jkurant,
If you only need the second activity to access the full width of the screen then instead of a 'custom dialog' you could just create a 'pane'l in the main activity which is made visible on clicking the 'Edit Reminder' button. The panel could be as big or as small as you like - All the panel would need is a few labels and EditText boxes and/or DateDialog & TimeDialog and a Button or two.

Douglas
 
Upvote 0

jkurant

Member
Licensed User
Longtime User
Okay, Douglas, are you saying that the panel would always be there, but is normally hidden? I would put my controls on that panel and just make it visible when I need it?

Interesting solution. I used to do that back in VB6. Thanks!
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
Yes - Exactly,
You could create the panel with the designer (easier), or by code - I tend to create using the Designer, and modify the panel with code at run-time depending on the device sreen size. Assuming you use the designer you simply follow the following logic, assuming the Panel is called 'PanelDialog' :-

LoadLayout("MyInputPanel") ' -> PanelDialog now has control
PanelMain.Enabled=False ' -> Disable background if PanelDialog is not full screen

Then make the exiting procedure

PanelDialog.Visible=False
PanelMain.Enabled=True ' -> Re-enable the main screen from.

The attached screenshots show a two level use of this method. Underneath the panels is a ListView, clicking an entry brings up contact details for the selected user (in this case me) - This panel uses TextEntry boxes as not all contacted details have international dialing codes, so the user can edit this if need be. Clicking the Text or Email Buttons brings up another Panel, offset to allowing the name from the first panel to remain visible and the entry if a text message or email. When finished the system rolls back the panels to the ListView.

Hope this helps
Douglas
 

Attachments

  • ScreenShot1.jpg
    11.3 KB · Views: 295
  • ScreenShot2.jpg
    10.5 KB · Views: 263
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…