Android的锁屏行为 [英] Android lock screen behaviour

查看:247
本文介绍了Android的锁屏行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我preSS家,回来我的应用程序稍后我会发现,状态一直$ P $完美pserved。出于某种原因,但是如果我锁定手机,然后打开它,我的应用程序已恢复到原来的状态栏在这里的几件事情有。当我看着我的日志发现,而手机是处于锁定状态的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?

推荐答案

这是Android操作系统的工作方式,它决定通过它自己的时候摧毁你的看法。为了避免丢失这些信息存在,可以在您的活动重新实现一个方法

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);
}

保存所有需要的数据转换成outState,并在onCreate方法,检查它的一个新的实例或保存的实例,如:

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);
}

这篇关于Android的锁屏行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆