在我的Android应用程序中,当我旋转设备(滑出键盘)时,“活动”将重新启动(调用onCreate)。现在,这可能是应该的,但我在onCreate方法中做了很多初始设置,所以我需要:

将所有初始设置放在另一个功能中,这样不会在设备旋转或使其不再调用Create,布局仅调整或将应用程序限制为仅肖像,以便不调用onCreate。


当前回答

我所做的。。。

在清单的活动部分中,添加了:

android:configChanges="keyboardHidden|orientation"

在活动代码中,实现:

//used in onCreate() and onConfigurationChanged() to set up the UI elements
public void InitializeUI()
{
    //get views from ID's
    this.textViewHeaderMainMessage = (TextView) this.findViewById(R.id.TextViewHeaderMainMessage);

    //etc... hook up click listeners, whatever you need from the Views
}

//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    InitializeUI();
}

//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);

    InitializeUI();
}

其他回答

修复AndroidManifest.xml中的屏幕方向(横向或纵向)

android:screenOrientation=“纵向”或android:scheenOrientation=“横向”

为此,不会调用onResume()方法。

有几种方法可以做到这一点:

保存活动状态

您可以在onSaveInstanceState中保存活动状态。

@Override
public void onSaveInstanceState(Bundle outState) {
    /*Save your data to be restored here
    Example: outState.putLong("time_state", time); , time is a long variable*/
    super.onSaveInstanceState(outState);
}

然后使用捆绑包恢复状态。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState!= null){
       /*When rotation occurs
        Example : time = savedInstanceState.getLong("time_state", 0); */
    } else {
      //When onCreate is called for the first time
    }
}

自行处理方向更改

另一种选择是自己处理方向更改。但这并不是一种好的做法。

将此添加到清单文件中。

android:configChanges="keyboardHidden|orientation"

对于Android 3.2及更高版本:

android:configChanges="keyboardHidden|orientation|screenSize"

@Override
public void onConfigurationChanged(Configuration config) {
    super.onConfigurationChanged(config);
    
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        //Handle rotation from landscape to portrait mode here
    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
        //Handle rotation from portrait to landscape mode here
    }
}

限制旋转

您还可以将活动限制在纵向或横向模式,以避免旋转。

将此添加到清单文件中的活动标记:

        android:screenOrientation="portrait"

或者在活动中以编程方式实现:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

使用应用程序类

根据您在初始化中所做的操作,您可以考虑创建一个扩展Application的新类,并将初始化代码移动到该类中的重写onCreate方法中。

public class MyApplicationClass extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    // TODO Put your application initialization code here.
  }
}

应用程序类中的onCreate仅在创建整个应用程序时调用,因此“活动”在方向或键盘可见性更改时重新启动不会触发它。

最好将这个类的实例公开为一个单例,并使用getter和setter公开正在初始化的应用程序变量。

注意:您需要在清单中指定要注册和使用的新Application类的名称:

<application
    android:name="com.you.yourapp.MyApplicationClass"

对配置更改作出反应[UPDATE:自API 13以来,这已被弃用;请参阅推荐的替代方案]

作为另一种选择,您可以让应用程序监听可能导致重新启动的事件(如方向和键盘可见性更改),并在“活动”中处理这些事件。

首先将android:configChanges节点添加到“活动”的清单节点

 <activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

或适用于Android 3.2(API级别13)及更高版本:

<activity android:name=".MyActivity"
      android:configChanges="keyboardHidden|orientation|screenSize"
      android:label="@string/app_name">

然后在“活动”中重写onConfigurationChanged方法,并调用setContentView以强制以新方向重新完成GUI布局。

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);
}

您可以使用此代码锁定屏幕的当前方向。。。

int currentOrientation =context.getResources().getConfiguration().orientation;
        if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
            ((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            ((Activity) context). setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

我发现这样做的方法是使用onRestoreInstanceState和onSaveInstanceState事件在Bundle中保存一些东西(即使您不需要保存任何变量,也只需在其中放置一些东西,这样Bundle就不会为空)。然后,在onCreate方法上,检查Bundle是否为空,如果为空,则进行初始化,如果不是,则执行初始化。