来电期间 activity 生命周期的奇怪行为:延迟 onStop()
Weird behavior of activity life cycle during incoming call : delayed onStop()
我有一个简单的 activity 没有 UI。我想在调用期间检查 activity 的生命周期方法。
当呼叫通知到达时,没有按预期发生任何事情。当我接听电话时,电话的 activity 会覆盖我的 activity。因此,理想情况下 onStop()
应该立即被调用。我检查了日志,只有 onPause()
在接受呼叫时被呼叫。但在 2-3 秒后 onStop()
也被调用。
Activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart: ");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop: ");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause: ");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume: ");
}
}
清单
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
日志
05-17 22:10:25.025 E/MainActivity: onStart:
05-17 22:10:25.054 E/MainActivity: onResume:
When call has been accepted:
05-17 22:10:34.405 E/MainActivity: onPause:
After 2-4 seconds:
05-17 22:10:38.144 E/MainActivity: onStop:
根据 onStop()
的 documentation :
Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
这里,另一个activity正在掩护我的activity并隐藏它。因此,onStop()
应该在 onPause()
之后立即调用。
我已经在 Moto G4 设备和 Nexus 5 模拟器上进行了测试。两者都表现出相同的行为。
延迟 onStop() 调用的原因是什么?
谁能解释一下内部细节?
截图
After receiving onPause()
call you will usually receive a following call to
onStop() (after the next activity has been resumed and displayed),
however in some cases there will be a direct call back to onResume()
without going through the stopped state.
尽管 onStop()
的文档有点令人费解,它声称一旦 Activity 不再可见就会调用它,但在它被调用之前有轻微的延迟(这取决于显示下一个activity。
如果您接到电话,在电话进入前台后会出现轻微的延迟。我在个人 phone 上也观察到这种延迟。此延迟将导致 onStop()
调用延迟。
When I accept the call, then the activity for the call is covering up on my activity. So, ideally onStop() should be called immediately.
接听电话不会从任务堆栈中删除 activity,并且在您重新进入应用程序时可用 => onPause() => onStop()。
所以这就是为什么在收到调用后不会立即调用 onStop 的原因。
If an activity has lost focus but is still visible (that is, a new
non-full-sized or transparent activity has focus on top of your
activity), it is paused. A paused activity is completely alive (it
maintains all state and member information and remains attached to the
window manager), but can be killed by the system in extreme low memory
situations
如果您在 activity A 之上打开 activity B,这就是生命周期
- onPause of activity A 被调用
- onCreate of activity B 被调用
- onStart activity B 被调用
- onResume of activity B 被调用
activity A 的 onStop 被调用(除了部分可见的 activity)
- 内存不足的情况下onDestroy的activity也可以调用
如您所见,在 activity A 的 onpause 和 onstop 之间调用了很多方法,因此 delay
序列是可预测的并记录在 Starting one activity from another
Coordinating activities :
Here's the order of operations that occur
when Activity A starts Activity B:
- Activity A's
onPause()
method executes.
- Activity B's
onCreate()
, onStart()
, and onResume()
methods execute in sequence. (Activity B now has user focus.)
- Then, if Activity A is no longer visible on screen, its
onStop()
method executes.
基本上,出现的 activity 是在消失的 onPause()
和 onStop()
回调之间创建并完全初始化的。
这可能需要一些时间,甚至可能根本不会调用 onStop()
方法,例如,如果新的 activity 使用对话框样式(因此旧的仍然部分可见)
我认为这与呼叫显示的实现方式有关。 onPause
当您的 activity 不再在前景中具有主要焦点时调用,但在技术上仍然是 "visible"。 onStop
当你的 activity 完全不可见时调用。
两个活动有可能具有可见状态,这将导致只有 onPause
被调用而 activity 没有焦点。通过在您的 activity 之上显示通话视图,您的 activity 可能仍然是 "visibile" 而不会完全消失。因此,为什么 onPause
被调用,而不是 onStop
.
我有一个简单的 activity 没有 UI。我想在调用期间检查 activity 的生命周期方法。
当呼叫通知到达时,没有按预期发生任何事情。当我接听电话时,电话的 activity 会覆盖我的 activity。因此,理想情况下 onStop()
应该立即被调用。我检查了日志,只有 onPause()
在接受呼叫时被呼叫。但在 2-3 秒后 onStop()
也被调用。
Activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart: ");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop: ");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause: ");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume: ");
}
}
清单
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
日志
05-17 22:10:25.025 E/MainActivity: onStart:
05-17 22:10:25.054 E/MainActivity: onResume:
When call has been accepted:
05-17 22:10:34.405 E/MainActivity: onPause:
After 2-4 seconds:
05-17 22:10:38.144 E/MainActivity: onStop:
根据 onStop()
的 documentation :
Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
这里,另一个activity正在掩护我的activity并隐藏它。因此,onStop()
应该在 onPause()
之后立即调用。
我已经在 Moto G4 设备和 Nexus 5 模拟器上进行了测试。两者都表现出相同的行为。 延迟 onStop() 调用的原因是什么?
谁能解释一下内部细节?
截图
After receiving
onPause()
call you will usually receive a following call to onStop() (after the next activity has been resumed and displayed), however in some cases there will be a direct call back to onResume() without going through the stopped state.
尽管 onStop()
的文档有点令人费解,它声称一旦 Activity 不再可见就会调用它,但在它被调用之前有轻微的延迟(这取决于显示下一个activity。
如果您接到电话,在电话进入前台后会出现轻微的延迟。我在个人 phone 上也观察到这种延迟。此延迟将导致 onStop()
调用延迟。
When I accept the call, then the activity for the call is covering up on my activity. So, ideally onStop() should be called immediately.
接听电话不会从任务堆栈中删除 activity,并且在您重新进入应用程序时可用 => onPause() => onStop()。
所以这就是为什么在收到调用后不会立即调用 onStop 的原因。
If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations
如果您在 activity A 之上打开 activity B,这就是生命周期
- onPause of activity A 被调用
- onCreate of activity B 被调用
- onStart activity B 被调用
- onResume of activity B 被调用 activity A 的 onStop 被调用(除了部分可见的 activity)
- 内存不足的情况下onDestroy的activity也可以调用
如您所见,在 activity A 的 onpause 和 onstop 之间调用了很多方法,因此 delay
序列是可预测的并记录在 Starting one activity from another
Coordinating activities :Here's the order of operations that occur when Activity A starts Activity B:
- Activity A's
onPause()
method executes.- Activity B's
onCreate()
,onStart()
, andonResume()
methods execute in sequence. (Activity B now has user focus.)- Then, if Activity A is no longer visible on screen, its
onStop()
method executes.
基本上,出现的 activity 是在消失的 onPause()
和 onStop()
回调之间创建并完全初始化的。
这可能需要一些时间,甚至可能根本不会调用 onStop()
方法,例如,如果新的 activity 使用对话框样式(因此旧的仍然部分可见)
我认为这与呼叫显示的实现方式有关。 onPause
当您的 activity 不再在前景中具有主要焦点时调用,但在技术上仍然是 "visible"。 onStop
当你的 activity 完全不可见时调用。
两个活动有可能具有可见状态,这将导致只有 onPause
被调用而 activity 没有焦点。通过在您的 activity 之上显示通话视图,您的 activity 可能仍然是 "visibile" 而不会完全消失。因此,为什么 onPause
被调用,而不是 onStop
.