Setoreientation problem?

DevBaby

Active Member
Licensed User
Longtime User
I have a single app, one activity and I both load and resize my layouts via code onto panels. I then place these panels however I want. I am using version B4A 1.92 (I believe).

In my Activity_Create sub, I first check the screen size to see if the device is a phone or a tablet (this works fine using code shared here on the forums by Erel). If the device is a Phone, I setorientation to Portrait, if a Tablet, then I setorientation to Landscape via the Phone library. I do this before anything else.

My initial activity orientation setting in the designer is set to BOTH.

Getting to the problem..

When using the phone and holding the phone either landscape or portrait, the app loads once in portrait…as it should… no problem.


When using the Tablet and holding the tablet landscape, the app loads once in landscape…as it should…no problem.

Now the problem

When using the Tablet and holding the tablet “portrait”, the app begins to load landscape, but only at the top half of the screen and skewed. This occurs for about 3 seconds. Then the screen goes blank and then loads the correct way (like when I hold the tablet landscape).

It appears that the app is starting to load and then stops (or fires an orientation change etc.), and then reloads and calls Activity_Create again and loads things the correct way). Why does this happen? I am setting the orientation before I load anything else (splashscreen etc).

Also, the landscape setting is the only setting that reloads (Activity_Create called) everything after I wake the device. Everything reloads as if the app initially wants to go portrait, but then changes to landscape. This does not happen when I force the device in portrait mode. I tested this issue on the same device (Kindle Fire), and I get a completely different “wake” result when forcing landscape vs. portrait in code.
 

DevBaby

Active Member
Licensed User
Longtime User
Thanks for your help, but how will the OS know which orientation to use based on the device size? I want portrait 'only' for phones and landscape 'only' for tablets.

The OS will force based on the way the device is held, not the device size.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
The way I tend to do it at moment, though this may turn out to be bad practice, is I add a dummy panel to the activity and in the designer set it's 'top' to 0 for phone layout, 10 for 7 inch and 20 for 10 inch variants. then test the panels .top in code.

Make the panel transparent and 'send to back' so it's not seen.
 
Upvote 0

DevBaby

Active Member
Licensed User
Longtime User
Thanks to all of your help, I found the solution.

If I am holding the tablet portrait, and then I tell the app to run landscape in code (during Activity_Create), then the OS will look to change the Activity. The OS will still process the original portrait Activity_Create by loading images in the portrait setting causing the skew given that my “resize” code is for landscape, and ignoring any msgbox calls, etc.

After the original portrait Activity_Create is done, then the OS will immediately destroy this Activity and process a new Activity based on the new landscape setting. Thus, loading everything the way I intended.

The fix is to first check to see if the user is holding the tablet portrait; if so, then tell the app to run landscape, but don’t process any loading of screens, images etc. This can be done with a Boolean variable in Activity_Create that fires the loading process only if the orientation is either set (forced) to landscape or if the user is already holding the tablet landscape.

This way the user only sees the screens loading once in the correct mode.

The opposite happens for my phone devices that I want to force “portrait.” If the user is holding the phone landscape, then I force to portrait and hold off the loading code until the OS launches the “portrait” orientation and the associated Activity_Create.

Everything loads fine now. Thanks again, I know my original question was not all that clear.
 
Upvote 0
Top