对方向变化Android应用程序重置,最好的方式来处理?最好的、应用程序、方向、方式

2023-09-05 11:28:57 作者:陪我笑陪我闹

所以,我想提出一个基本的国际象棋程序玩弄与Android编程,到目前为止,我学到了很多不同的一些元素,但是这一次,我失去了。

So I am making a basic chess app to play around with some various elements of android programming and so far I am learning a lot, but this time I am lost.

在仿真器的方向变化的活动被重置。根据我的研究同样的事情也会随时都可能发生应用程序暂停/中断,即。键盘的变化,电话,击中home键等。

When the orientation of the emulator changes the activity gets reset. Based on my research the same thing will happen anytime the application is paused/interrupted, ie. keyboard change, phone call, hitting the home key etc.

显然,这是不可行的有一盘棋不断复位,再一次让我觉得自己需要学习如何解决这个问题。

Obviously, it is not viable to have a chess game constantly reset, so once again I find myself needing to learn how to fix this problem.

我的研究带来了几个主要的东西,覆盖onPaused方法在我的活动,监听定位,在我的清单键盘的变化。(通过机器人:configChanges),使用parcelables的,或序列号

My research brings up a few main things, overriding the onPaused method in my Activity, listening for Orientation, Keyboard changes in my manifest (via android:configChanges), using Parcelables, or Serialization.

我已经看过了使用Pacelables了大量的样品code,但说实话这是太混乱了。也许明天回来用新鲜的眼光将是有益的,但​​现在我更加看parcelables的少意义上,它使。

I have looked up a lot of sample code using Pacelables, but to be honest it is too confusing. Maybe coming back tomorrow with fresh eyes will be beneficial, but right now the more I look at Parcelables the less sense it makes.

我的应用程序利用一个局对象,它具有64细胞对象(8×8的二维阵列中),并且每个单元具有一个件对象,无论是实际的片或如果空间是空的空。假设我使用任何Parcelable或序列号我假设我会Parcelize或序列化每一个类,主板,电池和一块。

My application utilizes a Board object, which has 64 Cell Objects(in an 8x8 2D array), and each cell has a Piece Object, either an actual piece or null if the space is empty. Assuming that I use either Parcelable or Serialization I am assuming that I would have to Parcelize or Serialize each class, Board, Cell, and Piece.

首先,是Parcelable或序列号甚至是正确的事情,在看这个问题?如果是的话要么是Parcelable或序列化preferred这个?与我是正确的假设,每个三个对象将必须瓜分/序列化?最后,没有任何人有一个链接到一个简单的了解Parcelable教程?任何事情来帮助我理解,并停止进一步的头痛在路​​上时,我的应用程序,甚至进一步扩大。

First and foremost, is Parcelable or Serialization even the right thing to be looking at for this problem? If so is either Parcelable or Serializable preferred for this? And am I correct in assuming that each of the three objects would have to be Parceled/Serialized? Finally, does anybody have a link to a simple to understand Parcelable tutorial? Anything to help me understand, and stop further headaches down the road when my application expands even further.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

在在&lt您的清单;活动>标签,你可以添加机器人:configChanges =方向| keyboardHidden,这将停止该活动从重装,并呼吁onConfigurationChanged(),而不是当方向改变或键盘被隐藏。

in your manifest in the <Activity> tag, you can add android:configChanges="orientation|keyboardHidden", this will stop the activity from reloading and call onConfigurationChanged() instead when the orientation is changed or the keyboard is hidden.

如果您需要进行调整时,这些事件的发生,你可以在你的活动覆盖onConfigurationChanged(),如果不是所有你需要做的就是将属性添加到清单。

If you need to make adjustments when either of these events happen, you can override onConfigurationChanged() in your activity, if not all you have to do is add the property to the manifest.

是这样的:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.myLayout);
}

很完善。