Android Question Screen Orientation Hides App

aaronk

Well-Known Member
Licensed User
Hi,

Just wondering if I am missing something..

I am trying to rotate the screen only when running on a 7" or above screen (so on phone size screens it should be portrait)

I am using this code:
B4X:
Sub Globals
    Dim phone1 As Phone
End Sub

Sub GetDevicePhysicalSize As Float
    Dim lv AsLayoutValues
    lv = GetDeviceLayoutValues
    Return Sqrt(Power(lv.Height / lv.Scale / 160, 2) + Power(lv.Width / lv.Scale / 160, 2))
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If GetDevicePhysicalSize > 6 Then
        If GetDevicePhysicalSize >= 8 Then
            '10" tablet
            Log("10inch")
            Activity.LoadLayout("TabletUI")
            phone1.SetScreenOrientation(0) ' Rotate to Landscape
        End If
        If GetDevicePhysicalSize < 8Then
             '7" tablet
            Log("7inch")
            Activity.LoadLayout("TabletUI")
            phone1.SetScreenOrientation(0) ' Rotate to Landscape
        End If
    End If
    ' less than 6" screen (such as a phone)
    If GetDevicePhysicalSize <= 6 Then
         'phone
         Log("phone")
         Activity.LoadLayout("PhoneUI")
    End If
End Sub
The problem I have is it works fine when running this on a phone (Nexus 5) however when I run this on my Nexus 7 it rotates the screen (landscape) but then the app disappears but it is running in background. (it just doesn't display the app user interface).

However, If my Nexus 7 is already Landscape before I run my app, it will work fine and the app doesn't disappear.

Is there something I am missing or is there a better way to do this ?
 

RandomCoder

Well-Known Member
Licensed User
Have you tried placing the same code in activity resume? I seem to recall having this problem before and it solved it for me.

Regards,
RandomCoder
 

aaronk

Well-Known Member
Licensed User
Have you tried placing the same code in activity resume? I seem to recall having this problem before and it solved it for me.
Regards,
RandomCoder
Just moved the code to Activity_Resume and the same issue happens.
Screen rotates and layout doesn't show and the Android Home screen displays but I do see things getting written to the Log in the IDE so I know it's still running my app in the background.

Try this
phone1.SetScreenOrientation(0)
Activity.LoadLayout("TabletUI")

instead of this
Activity.LoadLayout("TabletUI")
phone1.SetScreenOrientation(0)

Not tested.
I just swapped them around so it rotates the screen and then loads the layout but got the same result.
 

RandomCoder

Well-Known Member
Licensed User
There must be more going on here as you say it works if already in landscape?
I notice that GetDevicePhysicalSize is potentially called 4 times when using a 7" screen. This is not too bad in this instance as it is only a small function, but I would normally use a Select Case statement here as it would then only need to call the function once.
However, having said all that, I don't think it will solve your problem here. There must be something else happening?
 

aaronk

Well-Known Member
Licensed User
I did notice that it's logging '7inch' 2 times so I guess it's calling that code 2 times somehow, just need to look into why and maybe the cause of the issue..
 

aaronk

Well-Known Member
Licensed User
Can you upload a small project showing the problem so we could look at it.
I created a small demo and is attached.
This is only a demo of what I am doing but in the demo it's working fine.

In my app code for my project (which I don't want to upload) it's not working.
I guess there is something in my code that is making this issue happen and might need to look into it.

However in this demo code which is attached you will notice that it's calling my code 2 times somehow as it's logging '** 7inch **' 2 times, this could be the reason for my app to hide the activity but now since I created the demo project I am starting to think maybe it's not the case since the demo project works and I am using the same code.
 

Attachments

klaus

Expert
Licensed User
A few points:
1) You set in the Project Attributes #SupportedOrientations: portrait why ?
This means that with a tablet you first force the orientation to portrait and then set it to landscape. That's pobably threason why Activity_Resume is called twice.
You should set it to #SupportedOrientations: unspecified

2) You should not load layouts in Activity_Resume but in Activity_Create.
If the Activity is paused and comes back without calling Activity_Create you reload the layout again.

3) You don't need to calculate the physical size you can get it directly with
GetDeviceLayoutValues.ApproximateScreenSize

Try the code below:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If GetDeviceLayoutValues.ApproximateScreenSize > 6 Then
        phone1.SetScreenOrientation(0)
        Activity.LoadLayout("tablet")
        Log("** tablet **")
    Else
        phone1.SetScreenOrientation(1)
        Activity.LoadLayout("phone")
        Log("** phone **")
    End If
End Sub
If you need the test for 7' tablets you can add it.
 

aaronk

Well-Known Member
Licensed User
Ok, I worked it out!

@klaus I loaded your code into a small demo project and it also logged ** tablet ** 2 times as well. (I think I know why)
I now have the code in Activity_Create.

In my app (not the demo project but my real app) I noticed for some reason I had Activity.Finish in the Activity_Pause sub.
So what was happening was the page was loading but since it was rotating the screen it was running the code in Activity_Pause since the activity paused while it rotates the screen then it runs the code again.

I had in my Activity_Pause sub Activity.Finish which means while the screen rotates it would end the Activity and then will never show the layout since the activity had ended.

Now I removed the Activity.Finish from Activity_Pause and now it works just like it should.

I think it's also logging the ** tablet ** 2 times since it's running the code when the app first opens and then runs the code again after the screen rotates.

Thanks heaps for all that helped.
 
Top