返回按钮需要尝试 3 次
我正在开发一个 Android 应用程序,但不理解后退按钮。
有一个活动(例如 A1),通过单击按钮,用户可以转到另一个活动(例如 A2)。一旦用户完成 A2 活动,他单击后退按钮,返回到上一个活动 A1。所有文档都说,A1 此时将 onResume() 。
确实如此。但是,如果我在 A2 中,并更改屏幕方向(从横向到纵向,反之亦然),则会发生非常不同的情况。 A2 活动再次按预期布置到不同的屏幕方向。当我现在按 BACK 时,Activity A2 再次展开(屏幕方向没有变化)。再次按 BACK,再次导致 Activity A2 再次展开。第三次按背面可返回活动 A1。
我在这里做错了什么,我错过了什么?谢谢
彼得
I'm developing an android app, and not understanding the back button.
There is an Activity (say A1) from which by clicking a button, user goes to another Activity (say A2). Once the user has finished with A2 activity, he clicks back-button, to go back to previous activity A1. All the docs say, A1 will onResume() at this point.
And it does. However, if I am in A2, and change the screen orientation (from landscape to portrait or vice versa), then something very different happens. The A2 activity lays itself out again, into the different screen orientation as expected. When I press BACK now, the Activity A2 lays itself out again (no change to screen orientation). Pressing BACK again, again causes Activity A2 to lay itself out again. A THIRD press on back takes you back to Activity A1.
What am I doing wrong here, what am I missing? Thanks
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的问题的措辞不完全正确。我稍微简化了这个案例。我使用微调器(而不是按钮)来转移到下一个活动。
Spinner(和 Gallery)有一个严重的错误,在文档中没有提到 - 当用户物理单击 Spinner 控件时以及当框架代码首次布置 Spinner 时,将调用 OnItemSelectedListener 事件处理程序。因此,您的微调器处理代码必须确定事件是由用户选择触发还是由所布置的微调器触发。最简单的方法是使微调器中的第一个项目始终为“尚未进行选择”,并忽略该选择上的所有事件。
请参阅 Android Spinner 选择 和类似帖子。
就我而言,方向变化导致微调器再次布局,因此我从中获得了两个事件,第一个是布局事件,第二个来自先前选择的条目。 这导致启动了一个虚假的第二个 Activity,这意味着需要按 3 次后退按钮才能“返回”第一个 Activity。它实际上是在第一次按下时返回,然后旋转器触发了一个布局事件和一个常规事件,使我两次进入第二个活动。这在屏幕上看不到,但可以通过日志消息看到。
当您更改方向时,当前的活动将被销毁,并创建/启动一个新的活动。
当您更改方向并按后退键时,前一个 Activity 将从暂停堆栈的顶部弹出、销毁,并创建/启动该 Activity 的新版本。
当您更改屏幕方向时,旧方向的 Activity 永远不会保留。它将立即被销毁,或者如果它位于暂停堆栈的较低位置,则当到达顶部时它将被销毁。
My question was not phrased completely correctly. I slightly simplified the case. I am using a Spinner, not a Button, to transfer to the next Activity.
Spinner (and Gallery) have a gross bug, not mentioned in the docs - the OnItemSelectedListener event handler is called when a user physically clicks the spinner control, and also when a spinner is first laid out by the framework code. Your spinner handling code must therefore determine if an event was triggered by a user selection or by the spinner being laid out. The easiest way to do this is to make the first item in a Spinner always be "no selection made yet", and to ignore all events on that selection.
See Android Spinner selection and similar posts.
In my case, the orientation change caused the spinner to get laid out again, and I thus got two events from it, the first the layout event, the second from the previously selected entry. And that caused a bogus second activity to be started, and that meant that 3 presses of the back button were needed to "get back" to the first Activity. It was actually going back on the first press, then the spinner fired a layout event and a regular event putting me in the second Activity twice. That wasn't seen on the screen, but was seen using log messages.
When you change orientation, the current Activity is destroyed, and a new Activity created/started.
When you change orientation and press the back key, the previous Activity is popped from the top of the paused stack, destroyed, and a new version of that Activity created/started.
When you change screen orientations, the Activity in the old orientation is never kept. It will be destroyed immediately, or if it is lower down the Paused stack, it will be destroyed when it comes to the top.
您不处理配置更改。查看此链接,它可能会对您有所帮助。
当您将方向从纵向更改为横向或从横向更改为纵向时,如果您不处理配置更改,则会重新创建该活动。
you're not handling configuration changes. Check out this link it may help you.
When you change your orientation from portrait to landscape or landscape to portrait and if you are not handling configuration changes, then the activity is recreated.