gpt4 book ai didi

java - 来电期间 Activity 生命周期的异常行为 : delayed onStop()

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:16 27 4
gpt4 key购买 nike

我有一个没有 UI 的简单 Activity 。我想在调用期间检查 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: ");
}
}

list

   <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:

根据 documentation onStop() 的:

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 模拟器上对其进行了测试。两者都表现出相同的行为。delayed onStop() 调用的原因是什么?

谁能解释一下内部细节?

截图 call is running

最佳答案

基于documentation :

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 不再可见就会调用它,但在它被调用之前有轻微的延迟(这取决于 next正在显示的 Activity )。

如果您接到电话,则在电话进入前台后会出现轻微的延迟。我也在我的个人电话上观察到这种延迟。此延迟将导致 onStop() 调用延迟。

关于java - 来电期间 Activity 生命周期的异常行为 : delayed onStop(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50396700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com