简体   繁体   中英

Android onResume and onCreate

I have a question regarding the logic of the life cycle of activities:

在此输入图像描述

When I learned Android, setting up an Activity was always done in the onCreate() function. Now when I resume my Activity, there might be stuff to be done in the onResume() that has already been done in onCreate() . But then, why would we not just put all the stuff into onResume() ?

why would we not just put all the stuff into onResume()?

Well onCreate() is called when your Activity is created and you need to initialize some very important things of your application like your main layout!.

Because some staff you don't want to do all the time activity resumed (like findViewById), It can be done once. Activity can be resumed many times (with dialogs, for example). Do not waste resources.

OnCreate Method start when you open the activty for first time or open activity after kill app so you should locate all stuffs you need to initialize for once like setContentView or some variables. For example when you run your app with IDE , on create method will call But if after creating activity press home button activity will pause And then open the app with recent app to call onresume() method For better realize initialize all activity method with Log.i(tag,text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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