简体   繁体   English

追踪活动生命周期

[英]Tracking the activity life cycle

I have requirement wherein I have to display a lock screen if the user moves out of the application. 我有一个要求,如果用户离开应用程序,我必须显示一个锁定屏幕。

Hence, the structure is: Activity A extends Activity B. Wherein Activity B is the deciding activity : "was application in backgound". 因此,结构为:活动A扩展了活动B。其中活动B是决定性活动:“在背景中使用了”。 If so it launches the lock activity. 如果是这样,它将启动锁定活动。

Now, say I am on activity A and receive a phone call. 现在,假设我正在进行活动A,并接到一个电话。 Hence the app gets into the background. 因此,该应用程序进入后台。 When it resumes I can see the glimpse of Activity A for a fraction of second and then comes the lock activity. 当它恢复时,我可以看到活动A的一瞬间,然后是锁定活动。

Can there be any solution to avoid that glimpse of Activity A? 有什么解决方案可以避免对活动A的一瞥?

You can see the lifecycle of activity from official doc 您可以从官方文档中查看活动的生命周期

You are using activity B just to track whether activity is alive. 您正在使用活动B只是为了跟踪活动是否仍然存在。 I am not sure if it is necesary. 我不确定是否必要。

If activity goes to background onPause() method is called, it means activity is not visible (it might be both screen lock or home button pressed), and onResume() is called when activity is visible again. 如果活动进入后台,则会调用onPause()方法,这意味着活动不可见(可能是同时按下了屏幕锁定或主页按钮),并且当活动再次可见时会调用onResume() In Activity A if you override onPause method and launch your lock activity, it should work. 在活动A中,如果您重写onPause方法并启动锁定活动,则该活动应该起作用。 (Or set a boolean onPause and launch lock activity on resume(you might see Activiy A though) (或设置布尔值onPause并在简历上启动锁定活动(不过您可能会看到Activiy A)

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM