2014-06-05 95 views
2

我的应用程序在横向模式下运行。当我的手机进入睡眠状态时,onDestroy()然后onCreate()被自动调用.i已经声明android:configChanges =“orientation “和android:screenOrientation =”landscape“。请告诉我该怎么做,以避免此问题。 我也附上了下面的xml文件。当睡眠按钮被按下时,onDestroy和onCreate被调用

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="m.example.ghb1" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="18" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="m.example.ghb1.MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="landscape" 
     android:configChanges="orientation" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
      <activity android:name=".PlayActivity" 
       android:configChanges="orientation" 
       android:screenOrientation="landscape" 

      > 

    </activity> 
</application> 

</manifest> 
+1

和哪里是问题? – Opiatefuchs

+0

按睡眠按钮,你的意思是'显示关'按钮? – Sundeep

回答

0

当我的手机放在然后睡觉的onDestroy(),然后的onCreate()被自动调用。

Android是可以调用的onDestroy(摧毁你的应用程序和活动),只要它愿意,你需要准备你的应用程序这样的情况 - 这里是好文章:

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

我已经声明android:configChanges =“orientation”和android:screenOrientation =“landscape”。

这是一种常见的黑客攻击,防止机器人从重建活动,使用它的程序员经常忘记,每当它需要释放内存为其他系统可能会破坏activites在许多其他情形

0

的Android可以拨打onDestroy()任务。你可以做的是重写onSaveInstanceState(Bundle savedInstanceState),写你想改变的捆绑参数这样的应用程序的状态值:

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
    // Save UI state changes to the savedInstanceState. 
    // This bundle will be passed to onCreate if the process is 
    // killed and restarted. 
    savedInstanceState.putBoolean("isRequired", true); 
    savedInstanceState.putDouble("myDouble", 1.9); 
    savedInstanceState.putInt("myInt", 1); 
    savedInstanceState.putString("MyString", "Welcome back to Android"); 
    // etc. 
} 

的包基本上存储NVP(“名称 - 值对”)的方式地图,它会被传递到onCreate和onRestoreInstanceState,你会提取这样的值:

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    // Restore UI state from the savedInstanceState. 
    // This bundle has also been passed to onCreate. 
    boolean myBoolean = savedInstanceState.getBoolean("MyBoolean"); 
    double myDouble = savedInstanceState.getDouble("myDouble"); 
    int myInt = savedInstanceState.getInt("MyInt"); 
    String myString = savedInstanceState.getString("MyString"); 
} 
相关问题