简体   繁体   English

如何处理萤幕方向变更

[英]How to handle screen orientation change

I'm trying to handle screen orientation change for my android application but without success. 我正在尝试为我的Android应用程序处理屏幕方向更改,但没有成功。 Here's my manifest : 这是我的清单:

  <activity
        android:name="fr.ups.l3info.l3info_catchgameactivity.CatchGameActivity"
        android:label="@string/app_name" 
        android:screenOrientation="user"
        android:configChanges="orientation|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and I adde this function in my activity class : 我在活动类中添加了此功能:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

But when I change the screen orientation the application is recreated and this function is never executed. 但是,当我更改屏幕方向时,将重新创建该应用程序,并且永远不会执行此功能。 What I have done wrong? 我做错了什么? Thanks for your help. 谢谢你的帮助。

在清单清单android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection"使用此android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection"

You might want to read more about activity lifecycle. 您可能想了解有关活动生命周期的更多信息。 More you can find here ( http://developer.android.com/training/basics/activity-lifecycle/index.html ) 您可以在此处找到更多信息( http://developer.android.com/training/basics/activity-lifecycle/index.html

But more important is to get familiar with your activity state . 但更重要的是要熟悉您的活动状态

Method you are looking for is not onConfigurationChanged(Configuration newConfig) , but onSaveInstanceState(Bundle savedInstanceState) . 您要查找的方法不是onConfigurationChanged(Configuration newConfig) ,而是onSaveInstanceState(Bundle savedInstanceState)

Now in your OnCreate() method you will need to check if there is some state already saved, and then recreate that situation. 现在,在您的OnCreate()方法中,您需要检查是否已经保存了某些状态,然后重新创建该状态。

There is a very good explanation on this link: Saving Android Activity state using Save Instance State 在此链接上有一个很好的解释: 使用“保存实例状态”保存Android活动状态

But basically what you need to do is this: 但基本上您需要做的是:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

and then with that in mind: 然后考虑到这一点:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

STATE_SCORE and STATE_LEVEL are some public static final String values that are used to somehow label your stuff you want to save. STATE_SCORESTATE_LEVEL是一些public static final String值,用于以某种方式标记要保存的内容。

For example, if you have EditText view, and you type something in, then rotate your screen, that value will be lost. 例如,如果您具有EditText视图,然后键入内容,然后旋转屏幕,则该值将丢失。 But in your onSaveInstanceState() method you shall use that Bundle parameter and put value of your EditText view as a String. 但是,在onSaveInstanceState()方法中,应使用该Bundle参数并将EditText视图的值作为String放置。

With that saved, now you can get that value in your onCreate() method and set the value of your EditText view to it. 保存该内容后,现在您可以在onCreate()方法中获取该值,并为其设置EditText视图的值。

Hope this helps :) 希望这可以帮助 :)

If you're trying to define a "different layout" to your app when the orientation changes, just define a new layout give it the same name as your other layout and put it under res/layout-land . 如果您尝试在方向更改时为您的应用程序定义“不同的布局”,只需定义一个新的布局,使其名称与其他布局相同 ,然后将其置于res/layout-land

So that when your application recreates itself and calls setContentView(layout_name) within the onCreate function it will call the under res/layout-land and not the one under res/layout . 这样,当您的应用程序重新创建自己并在onCreate函数中调用setContentView(layout_name) ,它将调用下res/layout-land而不是下res/layout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM