什么 Android 事件只调用一次直到 Activity 被销毁?
我正在寻找一个答案(但我可能会问错误的问题)
问题-任何事件是否只被调用一次 TOTAL 直到活动被销毁?
我问这个问题是因为当我的用户将手机旋转到横向时, oncreate 和 onstart 都会被调用,从而导致某种重新加载。
我正在寻找一个可以将行为放入其中的事件,该事件只会运行 1 次(直到该活动被终止)
提前谢谢
I'm looking for a single answer (but I might be asking the wrong question)
Question- does any event only get called once TOTAL until an activity is destroyed?
I ask because when my user rotates the phone to landscape oncreate and onstart are both invoked causing a reload of sorts.
I'm looking for an event that I could put behavior into that would only get run 1x (until the activity is killed)
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它特定于 Activity,只需检查 onCreate 事件中的 savingInstanceState 参数。如果为空,则运行您的代码,如果不是,则您的代码已经运行。
示例:
第一次运行 onCreate 时,savedInstanceState 将始终为 null,之后将被填充。
If it is specific to the Activity just check your savedInstanceState parameter in the onCreate event. If it is null, run your code, if not, your code has already been run.
Example:
savedInstanceState will always be null when onCreate is run for the first time, and it will be populated thereafter.
您并没有真正指定您想要用它做什么,所以我不能保证这适合您的使用,但是
Application.onCreate
仅调用一次。You don't really specify what you're trying to do with it, so I can't guarantee this is appropriate for your use, but
Application.onCreate
is only called once.如果您想消除在方向更改时重新创建活动,您可以侦听清单中的配置更改。
然后您可以像这样覆盖 onConfigurationChanged:
重新创建布局,使其与新方向匹配,而无需重新创建整个活动。
If you want to eliminate the recreation of your activity on an orientationchange you can listen for configchanges in the manifest.
And then you can override onConfigurationChanged like so:
to recreate the layout so that it matches the new orientation, without recreating the entire activity.
检查 http://developer.android.com/guide/topics/resources/ runtime-changes.html 处理配置更改并维护它们之间的大量数据...如果您需要在配置更改之间维护的只是设置,您可以使用 onSavedInstanceState() 和 onRestoreInstanceState()回调和给定的捆绑包。
Check http://developer.android.com/guide/topics/resources/runtime-changes.html to handle configuration changes and to maintain your huge data between them...if all you need to maintain between the configuration change is just the settings,you can use the onSavedInstanceState() and onRestoreInstanceState() callbacks and the given bundles.