为什么代码在otCreate()而不是onresume()中工作

发布于 2025-02-07 03:26:31 字数 585 浏览 1 评论 0 原文

我就像Android编程中的中级。我决定深入研究活动生命周期方法,并意识到了一些事情,例如为什么方法toasts.show()()恢复活动后再次调用许多其他方法。如果这些方法在on创建中,那么为什么如果您喜欢转到另一个活动并返回,它仍然会给您吐司消息。让我举个例子。

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

{Your Initialization go here }


 //Toas message 
 Toast.makeText(code).show();
 }
 }

因此,想象一下,您将这项活动留给另一个活动,然后再回来...为什么它仍然显示敬酒信息。因为因为生命周期是 吞噬 俄罗斯 onstart on par Onstop ondestroy

,当您回到主动行动时, 俄罗斯 onstart。 因此,如果未调用ongreate ...那么吐司消息如何显示。 请有人应该帮助我回答我整天搜索但找不到答案。

I'm like an intermediate in android programming. I decided to take a deep dive into the Activity lifecycle methods and I realised something, like why do methods Toasts.show() and many other methods get called again when the activity is resumed. If the methods are in the onCreate so why then if you like go to another activity and return it will still give you a Toast message. Let me give an example.

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

{Your Initialization go here }


 //Toas message 
 Toast.makeText(code).show();
 }
 }

So imagine you leaving this activity for another one and then coming back... why does it still show the Toast message. Because since the lifecycle is
OnCreate
onResume
onStart
onPause
onstop
onDestroy

And when you come back to your MainActivity it calls the
onResume
onStart.
So if onCreate is not called...So how does the Toast message show.
Please someone should help me answer this I've searched all day but couldn't find answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高冷爸爸 2025-02-14 03:26:31

accoording to lifecycle of activity https://developer.android.com/guide/components/activities/activity-lifecycle, if there is no memory engouh the app process will kill, so when you back to your activity on created will call another time.

the image below for clarification:
enter image description here

诠释孤独 2025-02-14 03:26:31

当您的活动在后台进行时,其他应用需要内存。即使您的活动处于后台,系统也可能会释放空间。现在,如果您导航到您的活动,则由于内存要求而删除了保存的实例。 onstart被称为on resume。您可以在此处阅读更多有关它的信息: https:// https://developer.android.com/指南/组成部分/活动/活动 - 五体
并查看此图像以了解更好的理解: https:> https:https://指南/组件/images/activity_lifecycle.png

这种不确定性是我们现在主要将视图模型用于较大应用的原因之一。

When your activity goes in the background, and memory is required for other apps. It is possible that the system frees up space even though your activity is in the background with ONPAUSED state. and now if you navigate to your activity, since the saved instance is removed due to memory requirements. instead of ONRESUME, ONSTART is called. you can read more about it here: https://developer.android.com/guide/components/activities/activity-lifecycle
and see this image for better understanding: https://developer.android.com/guide/components/images/activity_lifecycle.png

This uncertainty is one of the reasons why we now mostly use view models for a sizeable app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文