进入睡眠模式(按下电源按钮)
17.005: E/MotherActivity(28940): onPause 被调用 17.025: E/MotherActivity(28940): onStop 被调用 17.315: E/MotherActivity(28940): onDestroy 被调用 17.365: E/GameTuto1Activity(28940): MainActivity 构造函数被调用 17.365: E/MotherActivity(28940): onCreate 被调用 17.695: E/MotherActivity(28940): onStart 被调用 17.705: E/MotherActivity(28940): onResume 被调用 17.705: E/MotherActivity(28940): onPause 被调用
从睡眠模式返回,解锁手机时
755: E/MotherActivity(28940): onResume 被调用 935: E/MotherActivity(28940): onPause 被调用 935: E/MotherActivity(28940): onStop 被调用
然后手机就会冻结,直到我杀掉该进程。但是为什么当我恢复我的应用程序(从睡眠模式返回)时会经历onPause和onStop,而在进入睡眠模式时会经历onCreate、OnStart、onResume、onPause呢?
如果我在Nexus7上做同样的事情,就不会发生这种情况,生命周期是“正常”的。下面是日志:
进入睡眠模式(按下电源按钮)
43.782: E/MotherActivity(19876): onPause 被调用 43.822: E/MotherActivity(19876): onStop 被调用
从睡眠模式返回,解锁手机时
50.142: E/MotherActivity(19876): onRestart 被调用 50.142: E/MotherActivity(19876): onStart 被调用 50.172: E/MotherActivity(19876): onResume 被调用
问题很奇怪和多样化(也许),但答案可能非常聪明。 所以如果您有什么想法呢? 它只发生在一个项目中,该项目有3个线程:2个循环线程(我在onPause中杀死)和一个由PoolExecutor管理的线程(也在onPause中被杀死)。如果我做一个简单的应用程序,它就不会重现。 Nexus5版本=4.4.2,nexus7版本=4.2.2 我知道,这个问题中没有代码,我无法分享我的项目。 但是提前感谢所有思考这个问题的人。 Mathias .... 五小时后: 答案的线索 ...... 在我的清单中,我的活动上有:
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
**android:screenOrientation="landscape"**
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
如果我删除 android:screenOrientation="landscape",它就能正常工作。
那么这是为什么呢?为什么我不能设置横向方向并忘记配置更改事件。
更糟糕的是,如果我监听方向配置更改:
android:configChanges="orientation"
onConfigurationChanged方法没有被调用!!!但是如果我使用: android:configChanges="orientation|screenSize" 并重写onConfiguration,它就可以工作了....
为什么,屏幕方向=横向不能处理屏幕尺寸配置更改。有没有另一种简洁的方法来处理这个混乱,并告诉系统,“伙计,我处于横向模式,请不要在我把手机放入睡眠模式时将我弄成纵向”。
如果这里有人知道,谢谢回答。
否则,这意味着如果您想使应用程序为纵向或横向,则需要在清单中添加android:configChanges="orientation|screenSize",并在Activity中重写onConfigurationChanged(什么也不做)。oO'