【发布时间】:2023-03-23 02:25:02
【问题描述】:
有没有人遇到FragmentActivity的问题,当自己调用finish()时,它只调用onPause(),不调用onStop()和onDestroy()。但是该活动在设备上是不可见的?
我没有看到任何其他错误日志。
根据android的activity生命周期,如果activity是不可见的,那么它应该调用onstop()。但事实并非如此。它发生在 Android 5.0 上。
我找到了原因,但我不明白为什么会这样。
A扩展了FragmentActivity,它定义了launchmode:singleTask。
B扩展Activity,在Manifest中定义了android:launchMode="singleInstance", android:theme="@android:style/Theme.NoDisplay"。
C扩展了FragmentActivity,在Manifest中定义了launchmode: singleTask, android:excludeFromRecents="true", android:taskAffinity="com.xxx.xxxx.xxx"。
D 是一个 serviceConnector 单例实例。什么处理来自绑定服务回调的事件。
场景是:
Step1 : A calls B with startActivity(intent).
Step2 : B calls serviceConnector to send event to the service process and finish itself. It has no UI. It is set as Theme.NoDisplay.
Step3 : Service callbacks init C if not exist yet and C handle service responses event.
Step4 : In one case, C calls B with startActivity(intent)
Step5 : B calls serviceConnection to send event. No UI. C will onPause and when B finsih itself, C onResume()
Step6: Service callbacks and found C already exist, no create new one. C handle service respsonses.
Step7: user click button on C Fragement activity, C calls finish itself, But only C got onPause(). C couldn't be finish, no onStop(), no onDestroy() is called.
我不明白 C 调用 B,B finsih 本身,C resume 和 C finish 自己稍后有问题。
【问题讨论】:
-
你是在调用 onPause() super.onPause() 中的超级方法吗
-
是的,调用了 super.onPause。我更新了我的问题。我找到了原因。但我不明白为什么。