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

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

问题描述

我的最终目标是让一个应用程序运行一个代码块(应用程序,而不是活动)被打开后(从主屏幕等退出)



根据活动生命周期,这应该是每个活动基础上的 onRestart()事件(至少我解释它)



onRestart() onResume()被调用是否回到应用程序内的活动(返回按钮)和应用程序被调用后。



给定这个图



我以这种方式解释: / p>


  • RED =应用程序内的活动之间的移动

  • BLUE =移至应用程式之外的活动



我的理解不正确?



EDIT (澄清具体用例)



我试图使用onRestart()来复制onCreate()中发现的一些安全逻辑(PIN验证),但它正在甚至当我按下应用程序中的后退按钮时也呼叫...

解决方案

这将是因为除非你使用Fragments您的应用程序中的屏幕是一项新的活动,当您点击后退按钮时,它将重新启动该页面的活动。



如果我了解您想要的内容做正确的你想把你的代码放在onCreate,而不是onRestart。



查看评论用于回答 p>

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 vs. OnResume - Android生命周期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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