Fragment Orientation Change - Better Test than for

2019-07-30 01:02发布

In 'Landscape' mode I have two FrameLayouts controlled by One Activity and utilizing two Fragments. In 'Portrait' mode I have one FrameLayout controlled by One Activity, on Selecting a Line I call another activity to display the detail using a detail fragment

In the 'Portrait' detail activity I have the following check for orientation in the onCreate() method.

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); return; }

The above works well but,

A problem arises though when I have a 'small' device. In this case in 'Landscape' mode I don't want the two fragment view, rather I want to behave like the 'portrait' view. However since the device is in fact in 'Landscape' when the detail activity launches it automatically finishes.

So the questions is what is the best way of handling this?

2条回答
三岁会撩人
2楼-- · 2019-07-30 01:55

Preform an additional check for screen size before your check for the orientation. Considering that a small device has a width of 500 pixels and a height of 600 pixels, you would do something like this.

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); i
int width = size.x; 
int height = size.y;
if ( width > 500 && height > 600 && 
    getResources().getConfiguration().orientation == 
    Configuration.ORIENTATION_LANDSCAPE)
{ 
     finish(); 
     return; 
}
查看更多
Bombasti
3楼-- · 2019-07-30 02:04

or create a custom resource with a bool value (from google io 2012)

<!-- in your values/custom.xml -->
<resources>
    <bool name="small_screen">true</bool>  
    <bool name="normal_screen">false</bool>  
</resources>

<!-- in your values-sw320dp/custom.xml -->
<resources>
    <bool name="small_screen">false</bool>  
    <bool name="normal_screen">true</bool>
</resources>

NOTE: You have to define a minimum screenwidth (sw320dp) for which you will consider a screen not to be small (link with more info)

The advantage is that you can read this value at runtime & you can have special cases for special resource qualifiers... E.g. you can read this value at runtime by calling in your activity:

if(getResources().getBoolean(R.bool.small_screen)) {
    // You have a screen which is < 320dp
} else {
    // You have a screen which is >= 320dp
}

You can even use this boolean resource in your manifest like so, to start a completely different activity for small screens

<activity android:name="SmallScreenActivity" 
          android:enabled="@bool/small_screen">  <!-- ENABLE FOR SMALL SCREEN -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name="NormalActivity" 
          android:enabled="@bool/normal_screen"> <!-- ENABLE FOR OTHER -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This way you can simply use an Activity for the normal case (android:enabled="@bool/normal_screen") and use a special activity for small screen android:enabled="@bool/small_screen"

WARNING: This method will not work on newer devices since honeycomb. You can read why this method is not allowed anymore or read about working similar solution

查看更多
登录 后发表回答