Orientation problem

HARRY

Active Member
Licensed User
Longtime User
Hallo everybody,

A program of me runs in landscape mode; it invokes that mode with the pachScreen library.

On the PDA the user can start another program while my program is running. This second program changes the orientation to portrait.

If I react on that orientation change in my program, still running in the background, with a reorientation of the screen to landscape I disturb that second program. Some programs could cause a loop if those programs also react on the orientation change.

Of course I could just remenber that orientation change, but I cannot use that 'flag', as I do not know in my program that I am back on the screen.

Am I thinking in circles or is this really a problem?

Harry
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using the door library you can add two new related events to a form:
- Activated, fired when the form gets focus.
- Deactivate, fired when the form loses focus.

You can use these events to do whatever you need when your application returns to the foreground.
It is pretty easy to end with circular calls with these events, so be careful (like showing a msgbox in the Activated event).

B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    obj.New1(False)
    obj.FromControl("form1")
    eventActivated.New1(obj.Value, "Activated")
    eventDeactivated.New1(obj.Value, "Deactivate")
End Sub

Sub eventActivated_NewEvent
    form1.Text = "activated: " & TimeS
End Sub
Sub eventDeactivated_NewEvent
    form1.Color = Rgb(Rnd(0,255), Rnd(0, 255), Rnd(0, 255))
End Sub
 

Attachments

  • 1.sbp
    811 bytes · Views: 217

HARRY

Active Member
Licensed User
Longtime User
Thanks Erel,

That's what I needed. It works fine. Does a way exist to do that for the collections of forms or do I have to implement that for each form individually?

Harry
 

HARRY

Active Member
Licensed User
Longtime User
Erel,

I don't understand how to mix the door library with AddEvent. Could you please give some sample code just for 2 forms which are activated. What to do in the activate routine is no problem .In practice the number of forms is ten.

Harry
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The attached code handles two forms. It is a bit more complex as it uses a timer to differentiate between a real deactivation / activation (Sub AppActivate / AppDeactivate) and just changing the visible form.
I haven't checked it on a device. Only on the desktop. So please report any problems you encounter.
B4X:
Sub Globals
    mode = ""
End Sub

Sub App_Start
    Form1.Show
    obj.New1(False)
    obj.FromControl("form1")
    eventActivated.New1(obj.Value, "Activated")
    eventDeactivated.New1(obj.Value, "Deactivate")
    obj.FromControl("form2")
    eventActivated2.New1(obj.Value, "Activated")
    eventDeactivated2.New1(obj.Value, "Deactivate")
    AddEvent("eventActivated", NewEvent, "FormActivate")
    AddEvent("eventActivated2", NewEvent, "FormActivate")
    AddEvent("eventDeactivated", NewEvent, "FormDeactivate")
    AddEvent("eventDeactivated2", NewEvent, "FormDeactivate")
    AddTimer("tmrA")
    tmrA.Interval = 10
End Sub
Sub AppActivate
    form1.Text = "activated: " & TimeS
End Sub
Sub AppDeactivate
    form1.Color = Rgb(Rnd(0,255), Rnd(0, 255), Rnd(0, 255))
End Sub
Sub FormActivate
    If mode = "deactivate" Then 'shifting between forms
        tmrA.Enabled = False
    Else
        mode = "activate"
        tmrA.Enabled = True
    End If
End Sub
Sub FormDeactivate
    If mode = "activate" Then 'shifting between forms
        tmrA.Enabled = False
    Else
        mode = "deactivate"
        tmrA.Enabled = True
    End If
End Sub
Sub tmrA_Tick
    tmrA.Enabled = False
    If mode = "activate" Then AppActivate Else AppDeactivate
    mode = ""
End Sub

Sub Button1_Click
    form2.Show
End Sub
 
Top