Search code examples
androidgame-enginelifecycle

Not suspending activities Android


I'm developing a video game for Android and I have a couple of doubts:

1) How can I make that the mobile doesn't suspend while I have the game running?

2) If I minimize my game and then open another, if I come back again to my game, the game restarts. I think Android destroys it because is not being used. What can I do to avoid this? I don't want that my game restarts if I open another.


Solution

  • 1) You can use a WAKE_LOCK. Look at the permission android.permission.WAKE_LOCK

    2) This time, look at the Android lifecycle. That can be a memory leak too, there is some tools to track that. You can find a tutorial on the Android blog. If there is no memory leak may be you use the "savedinstancestate" to run your game when you have paused. One more time, I recommand that tutorial from the documentation that explain how to save your activity state and recreate it.

    edit : More information about wakelock :

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
    wl.acquire();
    

    And mainly do not forget to release your wakelock

    wl.release();