操作方向改变自己方向、操作

2023-09-13 00:56:28 作者:恋上你的床

这是关于 Android的文档:configChanges ='方向'清单中的活动标签的属性:

  

请注意:使用这个属性应避免使用仅作为最后手段。请由于配置改变读取处理运行时更改了有关如何正确处理重新启动的详细信息。

为什么会这样说?

操作日志 市场调整模式和短线操作方向在哪里

在经由服务API库的线程和网络的请求的情况下,请求可以与一个参考原始活动制成,然后改变方向时可能发生,而使螺纹指向旧的活动。

虽然这可以是固定的,比例仅为处理配置更改自己是乏味和丑陋的。

为什么要回避?

编辑:我想我也应该问:这将是一个可以接受的理由这样做的方向配置更改自己

解决方案   

它为什么这么说呢?

由于他们希望你阅读文档的处理运行时更改部分。 : - )

  

在线程和网络的情况下,   通过服务API库的请求,一   请求可能与一个参考进行   到原来的活动,然后一个   可能发生方向改变,   而使螺纹指向旧   活动。

对于您关心的旋转,不要用含蓄地提及到活动案例(例如,定期的内部类),而是明确的(例如,静态内部类)。 这是一个全新的spankin'全新样本项目的证明了我的意思。

  

虽然这可以是固定的,它是乏味   和丑陋的比例仅为处理   配置改变自己。

的建议是存在的,我怀疑,因为他们害怕新来乍到Android将陷入困境处理配置改变自己。例如,他们决定有一些不同的字符串景观(你有更多的横向室),却忘了重新加载它们。或者,他们决定有一些不同的图像景观,忘记重新加载它们。等等。

大多数活动的大多数应用程序都不会有后台线程或插座或其他自己,要么是因为他们只是不需要他们,或者因为别的什么是管理他们(例如,服务)。他们的股票实施破坏和 - 重建典型的只是工程,特别是与内置支持保存 EditTexts 的窗口小部件的状态和这样的。

此外,您可能无法保存那么多的处理你自己,因为你的还是的需要实施的onSaveInstanceState()反正,处理方案比配置变更等(例如,你的活动被踢出内存以释放空间)。

现在,是他们的措辞有点苛刻?大概。经验丰富的Andr​​oid开发者可以自己决定哪个旋转处理策略聘请。我怀疑他们的口气是试图吓唬新人进入前都要沿着这条路三思而后行。

From the documentation regarding the android:configChanges='orientation' attribute of the activity tag in the manifest:

Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

Why does it say this?

In the case of threads and networking requests via a service API library, a request could be made with a reference to the original Activity, and then an orientation change could occur, leaving the thread pointing to the old Activity.

While this can be fixed, it's tedious and ugly compared to just handling the configuration changes yourself.

Why should it be avoided?

Edit: I guess I should also ask: would this be an acceptable reason for doing the orientation configuration changes yourself?

解决方案

Why does it say this?

Because they want you to read the "Handling Runtime Changes" section in the docs. :-)

In the case of threads and networking requests via a service API library, a request could be made with a reference to the original Activity, and then an orientation change could occur, leaving the thread pointing to the old Activity.

For cases where you care about rotations, don't use implicit references to the Activity (e.g., regular inner class), but rather explicit ones (e.g., static inner class). Here is a brand-spankin'-new sample project that demonstrates what I mean.

While this can be fixed, it's tedious and ugly compared to just handling the configuration changes yourself.

The recommendation is there, I suspect, because they are afraid newcomers to Android will mess up "handling the configuration changes yourself". For example, they decide to have some different strings for landscape (where you have more horizontal room) and forget to reload them. Or, they decide to have some different images for landscape and forget to reload them. And so on.

Most activities in most apps aren't going to have background threads or sockets or whatever of their own, either because they just don't need them, or because something else is managing them (e.g., a Service). Their stock implementation of destroy-and-recreate typically "just works", particularly with the built-in support for saving the widget state of EditTexts and such.

In addition, you may not save that much by "handling it yourself", because you still need to implement onSaveInstanceState() anyway, to handle scenarios other than configuration changes (e.g., your activity is kicked out of RAM to free up space).

Now, is their phrasing a bit harsh? Probably. Seasoned Android developers can make their own decisions as to which rotation handling strategy to employ. I suspect their tone is to try to scare newcomers into thinking twice before going down this route.