I just found that when the device has lock screen enabled, the followings happen. For this activity, android:screenOrientation="landscape" is set in the manifest. Then I perform the followings with my phone in a portrait mode.
我刚刚发现当设备启用锁定屏幕时,会发生以下情况。对于此活动,在清单中设置android:screenOrientation =“landscape”。然后我用手机以纵向模式执行以下操作。
- The user opens an activity.
- onCreated() is called
- onStart() is called
- onResume() is called
- The user LOCKS the device 4.5 onPause is called()
- onDestroy() is called
- onCreate() is called
- onStart() is called
- onResume() is called 8.5 onPause is called()
- The user UNLOCKS the device
- onResume() is called
- onDestroy() is called
- onCreate() is called
- onStart() is called
- onResume() is called.
用户打开一个活动。
调用onCreated()
onStart()被调用
onResume()被调用
用户LOCKS设备4.5 onPause被调用()
onDestroy()被调用
onCreate()被调用
onStart()被调用
onResume()被调用8.5 onPause被调用()
用户解锁设备
onResume()被调用
onDestroy()被调用
onCreate()被调用
onStart()被调用
onResume()被调用。
Okay, I don't understand why 6,7,8 are executed after the screen goes off.. Also I don't understand why 11, 12, 13, 14 are executed. Do some weird things happen when I lock and unlock the device? I am suddenly confused with the activity lifecycle.. Can anyone clarify this?
好吧,我不明白为什么在屏幕关闭后执行6,7,8 ..同样我不明白为什么要执行11,12,13,14。当我锁定和解锁设备时,会发生一些奇怪的事情吗?我突然对活动生命周期感到困惑..任何人都可以澄清一下吗?
Attache the code and the log msg
附上代码和日志消息
package com.example.wf;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("log", "oncreate");
}
@Override
protected void onResume() {
super.onResume();
Log.d("log", "onresume");
};
@Override
protected void onStart() {
super.onStart();
Log.d("log", "onstart");
};
@Override
protected void onPause() {
super.onStart();
Log.d("log", "onpause");
};
@Override
protected void onDestroy() {
Log.d("log", "ondestroy");
super.onDestroy();
};
}
Log msgs
10-05 23:11:07.994: D/log(23810): oncreate
10-05 23:11:07.994: D/log(23810): onstart
10-05 23:11:07.994: D/log(23810): onresume
// LOCK DEVICE
10-05 23:11:19.957: D/log(23810): ondestroy
10-05 23:11:20.007: D/log(23810): oncreate
10-05 23:11:20.007: D/log(23810): onstart
10-05 23:11:20.007: D/log(23810): onresume
// UNLOCK DEVICE
10-05 23:11:57.407: D/log(23810): onresume
10-05 23:11:57.537: D/log(23810): ondestroy
10-05 23:11:57.587: D/log(23810): oncreate
10-05 23:11:57.587: D/log(23810): onstart
10-05 23:11:57.587: D/log(23810): onresume
2 个解决方案
#1
40
On phones (or tablets with the orientation locked portrait), the lock screen is portrait only. Therefore when the device is locked, the device automatically switches to portrait mode (causing 6, 7, 8, and 9). When the device is unlocked, then onResume()
is called as your Activity
is becoming visible, but you are again transitioning between portrait and now locked in landscape, so the Activity gets destroyed and recreated in landscape.
在手机(或方向锁定肖像的平板电脑)上,锁定屏幕仅为纵向。因此,当设备锁定时,设备会自动切换到纵向模式(导致6,7,8和9)。当设备解锁时,当您的Activity变得可见时调用onResume(),但是您再次在纵向之间转换并且现在已锁定在横向中,因此活动将被破坏并在横向中重新创建。
#2
2
To over come of activity re-creation scenario, you can handle configuration changes at activity level by android manifest file using android:configChanges="orientation" attribute.
为了完成活动重新创建场景,您可以使用android:configChanges =“orientation”属性通过android清单文件处理活动级别的配置更改。
#1
40
On phones (or tablets with the orientation locked portrait), the lock screen is portrait only. Therefore when the device is locked, the device automatically switches to portrait mode (causing 6, 7, 8, and 9). When the device is unlocked, then onResume()
is called as your Activity
is becoming visible, but you are again transitioning between portrait and now locked in landscape, so the Activity gets destroyed and recreated in landscape.
在手机(或方向锁定肖像的平板电脑)上,锁定屏幕仅为纵向。因此,当设备锁定时,设备会自动切换到纵向模式(导致6,7,8和9)。当设备解锁时,当您的Activity变得可见时调用onResume(),但是您再次在纵向之间转换并且现在已锁定在横向中,因此活动将被破坏并在横向中重新创建。
#2
2
To over come of activity re-creation scenario, you can handle configuration changes at activity level by android manifest file using android:configChanges="orientation" attribute.
为了完成活动重新创建场景,您可以使用android:configChanges =“orientation”属性通过android清单文件处理活动级别的配置更改。