OnRestart与OnResume - Android的生命周期问题生命周期、问题、OnRestart、OnResume

2023-09-03 23:18:53 作者:戒不掉の思念

我的最终目标是要拥有,当它(该应用程序,而不是活动)被打开了被抛后运行的code块

据该活动的生命周期,这应该是在每个活动的基础上 onRestart()事件(至少我怎么跨preT吧)

两个 onRestart() onResume()被称为我是否重返该活动的应用程序中(后退按钮),当应用程序被称为回来了。

鉴于此图

我是跨preting这种方式:

红色在应用程序中的活动之间=移动 BLUE =移动到活动的应用程序之外

我的理解是不正确的?

修改(明确具体使用情况)

我尝试使用onRestart()来复制一些安全逻辑(PIN验证)中的onCreate()发现的,但它被称为甚至当我preSS后退按钮的应用程序内...

解决方案

这是因为当,除非你在你的应用程序中使用的碎片每一个屏幕是一个新的活动,当您单击后退按钮将重新启动的活动面前的页面。

如果我理解你想要做什么正确的,你想要把你的onCreate code,不是onRestart。

查看评论跟帖作为回答的

Activity和Intent

My end-goal is to have an application that runs a block of code when it (the application, not the activity) is opened up after being left ( back from home screen, etc... )

According to the Activity Lifecycle, this should be the onRestart() event on a per activity basis ( at least how I interpret it )

Both onRestart() and onResume() are being called whether I am returning to the Activity within the application (back button) AND when the app is called back up.

Given this diagram

I am interpreting it this way:

RED = movement between activities within the application BLUE = moving to an activity outside the Application

Is my understanding incorrect?

EDIT (Clarifying specific use case)

I'm attempting to use onRestart() to replicate some security logic (PIN Validation) found in onCreate(), but it's being called even when I press the back button inside the application...

解决方案

That would be because when unless your are using Fragments each "screen" in your application is a new activity, when you click the back button it restarts the activity of the page before it.

If I am understanding what you want to do correctly you want to put your code on onCreate, not onRestart.

SEE COMMENT THREAD FOR ANSWER