如何暂停和恢复安卓游戏?
我正在制作一款名为 Connect4 的 Android 游戏。我面临与暂停和恢复游戏有关的问题。当我在真实设备(Galaxy S2)上运行游戏时,在运行游戏时,当我按下电源按钮时,屏幕会被锁定,当我解锁屏幕时,我的游戏不会从我离开的位置恢复。基本上,我需要 3 个东西来恢复我的游戏:一个字节数组、一个字节和一个 FrameLayout 对象。这是我正在使用的代码:-
......
public void onSaveInstanceState(Bundle out){
super.onSaveInstanceState(out);
out.putParcelable("save", new Save());
}
public class Save implements Parcelable {
byte discs1, disc1[][];
FrameLayout root1;
Save(byte discs, byte[][] disc, FrameLayout root){
discs1=discs;
disc1=disc;
root1=root;
}
然后在 onCreate() 中我使用提供的包恢复它们,但这似乎不起作用。
I am making an android game called Connect4. I am facing a problem relating to pausing and resuming the game. When i run the game on a real device(on a Galaxy S2), while running the game, when i press the power button, the screen gets locked, and when i unlock the screen, my game does not resume from where i left it. Basically, i need 3 things to resume my game, a byte array, a byte, and a FrameLayout object. Here is the code I am using:-
......
public void onSaveInstanceState(Bundle out){
super.onSaveInstanceState(out);
out.putParcelable("save", new Save());
}
public class Save implements Parcelable {
byte discs1, disc1[][];
FrameLayout root1;
Save(byte discs, byte[][] disc, FrameLayout root){
discs1=discs;
disc1=disc;
root1=root;
}
Then in the onCreate() i restore them using the supplied bundle, but this not seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看活动生命周期。您需要使用代码覆盖
onPause()
、onResume()
等,以便在应用失去焦点时保存和恢复游戏。Have a look at the Activity Lifecycle. You'll need to overwrite
onPause()
,onResume()
etc. with code to save and restore your game when your app loses focus.您应该使用
onRestart()
函数。You should use the
onRestart()
function.