OnRestart与OnResume - Android的生命周期问题 [英] OnRestart vs. OnResume - Android Lifecycle Question

查看:939
本文介绍了OnRestart与OnResume - Android的生命周期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是要拥有,当它(该应用程序,而不是活动)被打开了被抛后运行的code块<应用程序(回程从主屏幕,等...)/ P>

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

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

鉴于此图

我是跨preting这种方式:

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

我的理解是不正确的?

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

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

解决方案

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

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

查看评论跟帖作为回答

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

这篇关于OnRestart与OnResume - Android的生命周期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆