我就像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.
发布评论
评论(2)
活动的活动,如果没有内存,请应用程序进程将杀死,因此,当您回到创建的活动时,将会再次致电。
下面的图像以进行澄清:
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:

当您的活动在后台进行时,其他应用需要内存。即使您的活动处于后台,系统也可能会释放空间。现在,如果您导航到您的活动,则由于内存要求而删除了保存的实例。 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.