Android生命周期哪个事件在生命周期中仅触发一次? [英] Android life cycle which event fired only once during the life cycle?
问题描述
我已经阅读了一些博客并访问了一些站点.我想知道哪个事件在生命周期中仅触发过一次.阅读博客后,我意识到onCreate()
方法在生命周期中仅被触发一次.我不知道我是对还是错.现在我的问题是,仅当我更改横向或纵向时,我才想触发任何一次触发的事件,而如果用户更改了方向而不是未触发事件,则在启动活动后,该事件未触发.我想知道在启动活动"后哪个事件仅一次触发.这个onCreate()
I have read some blogs and visited some site. I want to know which event fired only one time during the life cycle. After reading blogs I realize that onCreate()
method is fired only once during the life cycle. I don't know I am right or wrong. Now my problem is that I want to fired any event which fired one time only if I change the landscape orientation or portrait orientation than this event not fired means after starting activity if user change the orientation than event not fired. I want to know which event fired only one time after starting Activity. this onCreate()
推荐答案
onCreate
和onDestroy
id仅触发一次.
onCreate
and onDestroy
id fired only once.
onCreate:
在首次创建活动时调用.在这里,您应该进行所有常规的静态设置:创建视图,将数据绑定到列表等...
onCreate:
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc...
onDestroy
:在活动被销毁之前收到的最后一个呼叫.这可能是因为活动即将结束.
onDestroy
: The final call you receive before your activity is destroyed. This can happen either because the activity is finishing.
因此,将事件代码放在onCreate中.(但这取决于您的要求,您尝试执行的代码可能会更改)
第一个onCreate
被称为->下一个-> onStart
-> onResume
-> 您的正在运行"显示您的布局. (无论您输入了什么layout.xml)
first onCreate
is called --> Next --> onStart
--> onResume
--> your Activity is Running is show you your layout. (whatever you have put in your layout.xml)
现在,如果您按 HOME按钮,则它会转到-> onPause
-> onStop
. (活动不会破坏其在后台运行).现在再次打开活动",然后转到-> onRestart
-> onStart
-> onResumme
(活动再次运行).
now if you Press HOME Button then its goes to --> onPause
--> onStop
. (Activity is not Destroy its running in background). now again open Activity its go to --> onRestart
--> onStart
--> onResumme
(activity is running again).
现在,如果您按后退"按钮,则-> onPause
-> onStop
-> onDestroy
.
now if you Press Back Button then --> onPause
--> onStop
--> onDestroy
.
在使用方向更改时停止重新启动活动
android:configChanges="orientation|keyboardHidden"
.
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
如果您要开发API级别13或更高级别,则必须使用
if you developing for API level 13 or higher you must use
android:configChanges="orientation|screenSize"
这篇关于Android生命周期哪个事件在生命周期中仅触发一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!