当方向发生变化时会调用哪种活动方法?
当方向发生变化时,会调用生命周期的哪个方法? 我的应用程序执行 onResume()
方法,或者可能重新加载整个活动,因为我设置了一个布尔值来检查它是否是第一次运行。我读过 onConfigurationChanged()
在方向发生变化时启动,这是真的吗? 这要怎么处理呢?
Which method of the lifecycle is called when orientation changes occur?
My application executes the onResume()
method or maybe reloads the whole activity because I've set one boolean to check whether it is first run or not. I've read onConfigurationChanged()
starts when orientation change occur, is it true?
How to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的是...
Activity is start
onResume()
是您默认在 XML 中声明的。正如我从堆栈溢出的问题答案中发现的那样,
方向更改
Switch TO Activity 2
Orientation Change WHILE IN Activity 2
Switchback FROM Activity2
我猜测是因为 Activity 1 是在旋转时隐藏,
onRestoreInstanceState
不会被调用,因为有没有“视图”(即无法看到/查看)。此外,完全有可能有 2 个完全不同的纵向/横向布局文件,它们可能具有具有不同 ID 的不同 UI 元素。因此,我想说,如果您想在
onSaveInstanceState
中使用 Bundle 来保存自己的数据,那么您需要在onCreate
中添加额外的逻辑(在 Activity 中) 1) 在那里处理您自己的数据(以及在onRestoreInstanceState
中有条件地执行)。特别是,您可以维护一个“最后已知”方向字段,以便
onCreate
知道它需要处理您自己的数据,因为方向已更改,而不是依赖于onRestoreInstanceState
叫。Interesting one...
Activity is start
onResume()
is which you declare in your XML by default.And as I found from question answer on stack overflow is:
Orientation Change
Switch TO Activity 2
Orientation Change WHILE IN Activity 2
Switchback BACK FROM Activity2
I'm guessing that because Activity 1 is hidden at the time of rotation,
onRestoreInstanceState
isn't called because there is no 'view' (i.e., it can't be seen/viewed). Also, it is entirely possible to have 2 completely different layout files for portrait/landscape which potentially have different UI elements with different IDs.As a result, I'd say if you want to use the Bundle in
onSaveInstanceState
to save your own data, then you need to add extra logic in youronCreate
(in Activity 1) to process your own data there (as well as doing it conditionally inonRestoreInstanceState
).In particular, you could maintain a 'last known' orientation field so that
onCreate
knows that it needs to process your own data because orientation has changed, rather than relying ononRestoreInstanceState
being called.1
) 尝试在手机和/或模拟器上运行您的应用程序并打开 Logcat =>在窗口顶部选择详细。
2) 现在尝试更改屏幕方向(例如从纵向 => 横向模式)。
我希望这个替代方案能让您更深入地了解活动生命周期。
}
1) Try to run your app on your phone and/or emulator and open the Logcat => on top of the window select Verbose.
2) Now try to change the screen orientation (ex. from portrait => landscape mode).
I hope this alternative will give you more insight into the activity lifecycle.