Android 锁屏行为

发布于 2024-12-22 17:28:58 字数 180 浏览 3 评论 0原文

如果我按下主键并稍后返回我的应用程序,我会发现状态已被完美保留。然而,由于某种原因,如果我锁定手机然后解锁,我的应用程序就会返回到原始状态栏,其中有一些东西。当我查看日志时,我发现 onCreate 在手机处于锁定状态时被调用。因为锁定手机是一件很随意的事情,所以用户不希望每次这样做时都重置游戏。如何至少在锁定手机后几秒钟的时间内避免这种情况?

If I press home and come back to my app a little later I will find that the state has been preserved perfectly. For some reason however if I lock the phone and then unlock it, my app has been returned to the original state bar a few things here and there. When I looked into the logs I found that onCreate had been called while the phone was in a locked state. Because locking the phone is quite an off hand thing to do, having your game reset every time you do so is not desirable to the user. How can this be avoided at least for a longer period of time than a few seconds after locking the phone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情哥ㄟ 2024-12-29 17:28:58

这就是 Android 操作系统的工作原理,它自行决定何时销毁您的视图。为了避免丢失此信息,可以在您的活动中重新实现一个方法,

@Override
public void onSaveInstanceState(Bundle outState){
    iGameContent.saveGame(outState);
}

将所有需要的数据保存到 outState 中,并在 onCreate 方法中检查它是新实例还是已保存实例,如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    if (savedInstanceState!=null){
        iGameContent.loadGame(savedInstanceState);
    }else{
        // Normal initialization
    }
}

保存/加载的示例Bundle 的内容如下:

public void loadGame(Bundle aBundle){
    iBadsHit = aBundle.getInt("iBadsHits",0);
}

public void saveGame(Bundle aBundle){
aBundle.putInt("iBadsHit", iBadsHit);
}

This is how Android OS works, it decides by it's own when to destroy your view. To avoid loosing this information there is a Method that can be reimplemented in your activity

@Override
public void onSaveInstanceState(Bundle outState){
    iGameContent.saveGame(outState);
}

Save all your needed data into outState, and in the onCreate method, check if its a new instance or saved instance, like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    if (savedInstanceState!=null){
        iGameContent.loadGame(savedInstanceState);
    }else{
        // Normal initialization
    }
}

An example of the save/load to a Bundle is the following:

public void loadGame(Bundle aBundle){
    iBadsHit = aBundle.getInt("iBadsHits",0);
}

public void saveGame(Bundle aBundle){
aBundle.putInt("iBadsHit", iBadsHit);
}
屋顶上的小猫咪 2024-12-29 17:28:58

如果您的日志显示 onCreate 已被调用,则意味着您的应用程序进程已被终止。

你知道Android的Activity生命周期吗?如果没有,请在此处阅读:Android 活动

If your log is showing that onCreate has been called then that means your apps process was killed.

Do you know the Android Activity Lifecycle? If not, read up on it here: Android Activities

薆情海 2024-12-29 17:28:58

屏幕锁定的行为可能因设备而异。某些事件可能会导致应用程序遭到破坏。您可以尝试处理其中一些事件,以避免这种情况,并在AndroidManifest.xml中指定它:

android:configChanges="keyboardHidden|orientation"

这两个是屏幕锁定中最有问题的。您可以在 nvidia 文档的最后一章找到更多信息

The behavior on screen lock could vary from one device to other. Some events could cause the destruction of the app. You can try to handle some of this events to avoid this situation specifying it on the AndroidManifest.xml:

android:configChanges="keyboardHidden|orientation"

These two are the most problematic in screen lock. Yo can find more information on the last chapter of this nvidia document

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文