MeanEYE
MeanEYE

Reputation: 967

Prevent restarting landscape Android application on lock

I am developing a game (sort of) which uses OpenGL and has background music. Application runs only in landscape mode.

Problem I have, happens when user locks screen. At that point onStop() is called, screen orientation is changed and soon after my Activity is restarted as a result of orientation change. This means OpenGL surface is recreated, music starts playing again and all sorts of other things. This is counter-intuitive as locking screen should suspend CPU intensive operations, not start them.

Adding android:configChanges="orientation" to manifest did nothing. I do realize that I am probably missing screenSize in android:configChanges but I can not add it since I am targeting earlier API versions which do not have this option available.

So, question is: What's the proper way to handle this? On which event should I initialize my application so that everything works as expected?

Upvotes: 3

Views: 1732

Answers (6)

Mohan Krishna
Mohan Krishna

Reputation: 362

Add the tag

"android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout" for your activity in Manifest.xml,

And Override the method

public void onConfigurationChanged(Configuration newConfig) {}

Upvotes: 2

leenephi
leenephi

Reputation: 900

I think you will need to accept the fact that when the phone locks, your Activity is paused and/or stopped. This means that you will need to start some of those things again. That's also what I understand after reading an Android Game development book with OpenGL.

What you can do is a bit more than what @421 suggested. In those onSaveInstanceState and onRestoreInstanceState, you can store the part of the song you are on and any other data that will allow the game to start again "smoothly" even though it may have to reload some things.

If you haven't found any other answer by now, this may be why. I've experienced many annoyances with games doing weird things once the phone locks. You just need to manually handle it correctly and smoothly.

Upvotes: 1

shkschneider
shkschneider

Reputation: 18243

In your precise case I prefere to use this:

@Override
protected void onCreate(final Bundle savedInstance) {
    super.onCreate(savedInstance);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Works in all cases and forces your activity to be ONLY landscape.

Upvotes: 1

Meher
Meher

Reputation: 2585

        @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
}

Above are two methods in activity to save the state when orientation of activity changes.It saves data in the bundle object in the form of key value pairs.

No need to add any tag in Androidmanifest.xml file. for the above piece of code.

android:screenOrientation="landscape" this tag can be used to make your activity to run only in Landscape mode.This tag should be added activity tag present in Androidmanifest.xml

Upvotes: 1

Ali
Ali

Reputation: 22337

Adding screenSize to configChanges in manifest doesn't mean that your program won't run on older version. You just need to set targetSdkVersion to higher version and minSdkVersion to whatever you like. In intelljIDEA I set ModuleSKD to 4 in Project Structure so it doesn't highlight screenSize as invalid. It must be similar in Eclipse.

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />

<activity android:name=".MyActivity"
      android:label="@string/app_name"
      android:configChanges="orientation|screenSize|keyboard|keyboardHidden|navigation">

Also you can check the version in code like this:

if (Build.VERSION.SDK_INT >= 11) 

This way your app work on 2.2 as well as higher versions.

I think you won't need onConfigurationChanged because your game probably run only in landscape mode but if your interested have a look at my question very similar to yours and my own answer.

replace layout on orientation change

Upvotes: 5

zasadnyy
zasadnyy

Reputation: 2167

If your game is only for landscape mode, try to set android:screenOrientation="landscape" property in activity tag.

Upvotes: 1

Related Questions