Skip to content Skip to sidebar Skip to footer

Android Lock Screen Behaviour

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 a

Solution 1:

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

@OverridepublicvoidonSaveInstanceState(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:

publicvoidloadGame(Bundle aBundle){
    iBadsHit = aBundle.getInt("iBadsHits",0);
}

publicvoidsaveGame(Bundle aBundle){
aBundle.putInt("iBadsHit", iBadsHit);
}

Solution 2:

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

Solution 3:

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

Post a Comment for "Android Lock Screen Behaviour"