简体   繁体   English

了解活动生命周期

[英]Understanding activity lifecycle

I am just starting my experience with Android development (I am watching tutorials right now).我刚刚开始体验 Android 开发(我现在正在看教程)。 I looked at the activity lifecycle on the Android developers page, and I realized that the activity always goes through the onResume() method before it's visible to the user.我查看了 Android 开发人员页面上的 Activity 生命周期,我意识到 Activity 在用户可见之前总是通过onResume()方法。 Assuming I will be using no fragments in the activity, does that mean that most of the code logic should be within the onResume() method, and I should just inflate the layout once inside the onCreate() at the beginning?假设我将在活动中不使用任何片段,这是否意味着大部分代码逻辑应该在onResume()方法中,而我应该在开始时在onCreate()内部膨胀一次布局?

Please Refer the site for the better understanding of the activity lifecycle https://developer.android.com/guide/components/activities/activity-lifecycle and also this for brief understanding https://www.javatpoint.com/android-life-cycle-of-activity请参阅该站点以更好地了解活动生命周期https://developer.android.com/guide/components/activities/activity-lifecycle以及此简要了解https://www.javatpoint.com/android-life -活动周期

Now answering your question onCreate() is not just for inflating the layout.现在回答你的问题onCreate()不仅仅是为了夸大布局。 The main part of the core logic is written here and onResume() is called when you minimize the or open the app once again it is called again and again but onCreate() is called once untill and unless the control is not forwarded to another activity核心逻辑的主要部分写在这里,当您最小化或再次打开应用程序时,会调用onResume()它会一次又一次地被调用,但 onCreate() 会被调用一次,除非控制未转发到另一个活动

Like in Java the start running from public static void main(String[] args){ }就像在 Java 中一样,从 public static void main(String[] args){ } 开始运行

In Android(Activity) the first line will be executed will be from onCreate() and not from the onResume()在 Android(Activity) 中,第一行将来自 onCreate() 而不是来自 onResume()

if you will practice the same and will habitual of this process again and again then you better understand what im trying to tell nothing can be more useful than you practice and your understanding try to print the toast or Log on each and every state of the activity lifecycle and you better understand this without the help of anyone如果你会练习同样的,并且会一次又一次地习惯这个过程,那么你会更好地理解我试图告诉什么比你练习更有用,你的理解尝试打印吐司或登录活动的每个状态生命周期,你可以在没有任何人帮助的情况下更好地理解这一点

Cheers Happy Coding!干杯快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM