繁体   English   中英

锁定手机屏幕后,活动再次开始

[英]Activity starts again when lock phone screen

我有下一个问题。 我正在开发游戏。 当我从物理按钮锁定设备并解锁时,游戏会再次开始。 活动再次开始。 解锁后,我想从锁定那一刻起继续播放。

然后,您需要将状态保存在onPause中,然后在onResume中再次加载

您需要使用onSaveInstanceStateonRestoreInstanceState 保存和恢复活动状态

在此处输入图片说明

救:

 static final String STATE_SCORE = "playerScore"; static final String STATE_LEVEL = "playerLevel"; ... @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(STATE_SCORE, mCurrentScore); savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); } 

恢复:

 public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM