How to change screen orientation, without creating a new activity on android?

折月煮酒 提交于 2019-12-07 08:08:40

问题


I do not know if the title is correct, here is what happens.

I have an application which works differently on a phone and on a tablet, on a phone it shows as portrait on a tablet it shows as landscape.

To achieve this I created a class called CoreActivity which is extended by all my activities and does the following:

public class CoreActivity extends Activity {
    protected boolean _landscape = false;

    public boolean isPhone() {
        int layoutSize = getScreenLayoutSize();
        return (layoutSize == Configuration.SCREENLAYOUT_SIZE_SMALL || layoutSize == Configuration.SCREENLAYOUT_SIZE_NORMAL);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isPhone() && !_landscape) {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }


    protected int getScreenLayoutSize() {
        return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
    }
}

My problem occurs when I wish to show a screen on a phone setup on landscape mode, to do this I use the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    _landscape = true
    super.onCreate(savedInstanceState);
}

The problem is, that on a phone, if the user is holding the phone on portrait mode (as one would, since most of the application is in portrait mode) then the activity gets created and destroyed and then recreated. But if they are holding it on landscape mode, then it is only created once.

My problem occurs because on the onCreate method I launch the threads that load data, and I also show fragments.

Is there a way to avoid this problem? is there a way to launch an activity from the start on portrait mode and not change it, or have it not create twice?

Thanks in advance for any help you can provide


回答1:


Recreating occurs just because you are forcing to, by calling setRequestedOrientation probably in order to set screen orientation. However you don't need to check and change it by code. You can do it via xml file. You can set different xml files depending on the screen size. But it is little bit hack so there is no guarantee in the future.

On the manifest file you can force by:

<activity
   android:name="com.my.example.MyActivity"
   android:screenOrientation="landscape"/>  // or portrait

So as far as I understand you want to force portrait for small sizes and landscape(or sensor) for larger screen sizes. But above configuration applies for all screen sizes. Here is the tricky part: landscape portrait sensor etc. are all integers defined here. As you might guess you can write android:screenOrientation=0 instead of landscape. What we know from beginning lessons of android, we can define integers in xml files then their values might vary on screen size. So..

You should first create different integers.xml files for different screen sizes. i.e.:

values
 - integers.xml
values-large
 - integers.xml

for values/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">1</integer>  // 1 for portrait
</resources>

for values-large/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="orientation">0</integer> // 0 for landscape
</resources>

and finally you you have to manipulate manifest file by:

<activity
   android:name="com.my.example.MyActivity"
   android:screenOrientation="@integer/orientation"/>  // read constant from xml files

You can also use sensor , fullSensor, nosensor, locked, behind, reverseLandscape, reversePortait options too. But I am warning you, this is a hack solution.




回答2:


Well, I found a solution to this issue, just declare:

android:screenOrientation="locked"

on every activity having this issue in the manifest.

And keep using setRequestedOrientation() programatically to define if landscape or portrait orientation within onCreate() method,

It will work! ;)




回答3:


try this in your manifest.xml... this will stop the recalling the onCreate() multiple time while orientation changes...

android:configChanges="keyboardHidden|orientation|screenSize"



回答4:


You dont need to handle this manually. Android has built it support for different screen sizes

check this link

http://developer.android.com/training/multiscreen/screensizes.html




回答5:


Did you try adding android:configChanges="keyboard|keyboardHidden|orientation" to the <activity>-Tag inside of your AndroidManifest.xml?

This should prevent the system from restarting the Activity, but I am not sure whether this will actually work when forcing the orientation programatically. It's worth a shot though.




回答6:


If all your activites must have the same orientation on a device, you can start your application with a splash screen activity, set the orientation like in your current code and forward to your CoreActivity or any other activity in your App.

In your AndroidManifest.xml set

<activity
    android:name=".CoreActivity"
    android:screenOrientation="behind"/>

This will use the same orientation as the activity that's immediately beneath it in the activity stack.



来源:https://stackoverflow.com/questions/27015688/how-to-change-screen-orientation-without-creating-a-new-activity-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!