Android Question Activity_Create and rotation

Semen Matusovskiy

Well-Known Member
Licensed User
Hi, guys --

An activity is designed as 'help' and includes toolbar and webview only.
My goal is to do like in Chrome - rotation should not re-create views. This allows, for instance, do not stop video inside web page.

I found how to prevent Activity_Create (to add something like SetActivityAttribute (helpmenu, android:configChanges, "keyboard|keyboardHidden|orientation|screenSize") into manifest.

But now it's interesting how to receive a event that orientation is changed ? Additional line in manifest prevents not only Activity_Create, but Activity_Resume also.

Guess, I can check orientation and to change layout by timer. Any better ideas ?

PS. Found - ime_HeightChanged detects screen size changes even without Activity_Create/Resume.
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
I don't think stopping Activity_Create will help because the OS will destroy the activity on the configuration change - so if you stop Activity_Create from running your app won't restart properly (if at all).

I think the general idea is that you should make sure the current state of your app is always saved, then if there is a screen rotation (or other configuration change) that requires the OS to restart the app, you can restore the state in your Activity_Resume.

You might find this link useful -> https://developer.android.com/guide/topics/resources/runtime-changes

- Colin.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
found some interesting about here
https://android.jlelse.eu/handling-orientation-changes-in-android-7072958c442a

B4X:
<activity
    android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>

Prevent Activity to recreated
Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest.xml. Using this attribute your Activities won’t be recreated and all your views and data will still be there after orientation change.

This attribute informs to Android system that you are going to handle orientation and screenSize changes for this Activity. So instead of destroying and recreating your Activity, Android will just rotate the screen and invoke one of the lifecycle callback method which is onConfigurationChanged(Configuration).

Note: In case you want to do something like display different layout then you have to implement onConfigurationChanged(Configuration) method and before inflating the new layout, you would require to manually discard the old layout. To use android:configChanges attribute is also not recommended by Android.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I added
1) android:configChanges to manifest (this prevents Activity_Create)
2) ime_HeightChanged (to detect orientation changes)

All is fine , except troubles with Activity.Width and Activity.Height. Guess, B4A calculates these values in onCreate event. At least Activity.Width and Activity.Height are not changed after rotation.

GetDeviceLayout returns correct screen width and height. Probably, it uses getWindow ().getDecorView ().getWidth (); and getWindow ().getDecorView ().getHeight (); . But I need exactly Activity.Width and Height (visible size of activity). Any ideas ?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I added
1) android:configChanges to manifest (this prevents Activity_Create)

I don't think it prevents Activity_Create from firing. I think what it actually does is stops the OS from destroying the activity in the first place. Also, not that using this attribute is not recommended by Android.

But I need exactly Activity.Width and Height (visible size of activity). Any ideas ?

Err - have you tried calling Activity.Width & Activity.Height or 100%x & 100%y?

- Colin.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Portrait (create event is present)
GetDeviceLayout : 1080 * 1920
Activity.Width (100%x) = 1080
Activity.Height (100%y) = 1845

Landscape (no create/resume events)
GetDeviceLayout : 1920 * 1080
Activity.Width (100%x) = 1080
Activity.Height (100%y) = 1845

Probably, it's possible to use following
B4X:
import android.graphics.Rect;

    public int getActivityHeight () 
    {
    Rect rectangle = new Rect();   
    getWindow().getDecorView().getWindowVisibleDisplayFrame (rectangle);
    return rectangle.bottom - rectangle.top; 
    }

And analog for width. Need to test ...
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Portrait (create event is present)
GetDeviceLayout : 1080 * 1920
Activity.Width (100%x) = 1080
Activity.Height (100%y) = 1845

Landscape (no create/resume events)
GetDeviceLayout : 1920 * 1080
Activity.Width (100%x) = 1080
Activity.Height (100%y) = 1845

Probably, it's possible to use following
B4X:
import android.graphics.Rect;

    public int getActivityHeight ()
    {
    Rect rectangle = new Rect();  
    getWindow().getDecorView().getWindowVisibleDisplayFrame (rectangle);
    return rectangle.bottom - rectangle.top;
    }

And analog for width. Need to test ...

Sorry - not understanding what the issue is with Activity.Width & Activity.Height? You said you wanted the visible size of the activity, which is what you're getting. If you want to know the orientation, I guess you could use getDeviceLayoutValues.Width & getDeviceLayoutValues.Height & if Width > Height then you're in landscape.

- Colin.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
B4A returns the same 1080 (width) * 1845 (height) in portrait and landscape, what is obviously wrong.
But, ok, just now I can retrieve correct values using inline Java.

Really the problem is more deep. In landscape I try to increase toolbar width, using correct value (1920 pixels).
But on the screen the width remains 1080 pixels.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
I am not really sure about your problem but this thread might help...
Have a look at the "AfterChange" sub and the "onConfigurationChanged" method in the inline java-code....
 
Last edited:
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
moster67 --

I liked onConfigurationChanged (better than ime_heightchanged)
But this event happens too early, when I can't change activity layout parameters.
I made a small delay (Sleep (1)) after this event and everything works. But Sleep (1) is a bad style. Maybe there are events "after all changes" ?
 
Last edited:
Upvote 0
Top