1

I'm trying to handle screen orientation change for my android application but without success. Here's my manifest :

  <activity
        android:name="fr.ups.l3info.l3info_catchgameactivity.CatchGameActivity"
        android:label="@string/app_name" 
        android:screenOrientation="user"
        android:configChanges="orientation|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and I adde this function in my activity class :

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

But when I change the screen orientation the application is recreated and this function is never executed. What I have done wrong? Thanks for your help.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Hunsu
  • 3,281
  • 7
  • 29
  • 64

3 Answers3

3

Use this in your manifest android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection"

Jorge Alfaro
  • 924
  • 8
  • 11
  • This is a last resource solution and should be avoided as possible – nKn Mar 24 '14 at 19:42
  • It's worked. but all the tutorials I have read say to just add android:configChanges="orientation|keyboardHidden" – Hunsu Mar 24 '14 at 19:44
  • the documentation says 'If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.' – Jorge Alfaro Mar 24 '14 at 19:47
2

You might want to read more about activity lifecycle. More you can find here (http://developer.android.com/training/basics/activity-lifecycle/index.html)

But more important is to get familiar with your activity state.

Method you are looking for is not onConfigurationChanged(Configuration newConfig), but onSaveInstanceState(Bundle savedInstanceState).

Now in your OnCreate() method you will need to check if there is some state already saved, and then recreate that situation.

There is a very good explanation on this link: Saving Android Activity state using Save Instance State

But basically what you need to do is this:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

and then with that in mind:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

STATE_SCORE and STATE_LEVEL are some public static final String values that are used to somehow label your stuff you want to save.

For example, if you have EditText view, and you type something in, then rotate your screen, that value will be lost. But in your onSaveInstanceState() method you shall use that Bundle parameter and put value of your EditText view as a String.

With that saved, now you can get that value in your onCreate() method and set the value of your EditText view to it.

Hope this helps :)

Community
  • 1
  • 1
Nikola Milutinovic
  • 731
  • 1
  • 8
  • 23
  • Yes I have read that before. The problem I encountered is that I can't save object (I can but hard to implement). – Hunsu Mar 24 '14 at 19:55
  • Do you really need to have screen rotation. If not you may lock the rotation by adding the rotation property inside your manifest. But if you need to have rotation, there is one more trick. Let me know what you need. – Nikola Milutinovic Mar 24 '14 at 19:57
  • Yes I need screen rotation – Hunsu Mar 24 '14 at 20:00
  • If you are using MVC model, maybe solution to your problem is to save your whole controller. One solution to that problem is to save it in a file. But your controller needs to implement `Serializable`, and all of its fields need to implement `Serializable` too. If your controller or whatever class you are trying to save has a View in it this is not possible, but also having the View inside the controller is wrong also. Let me know if you need any assistance with this. Some more code of yours would help a lot. – Nikola Milutinovic Mar 24 '14 at 20:04
  • Thanks for you help. I'm not working on a complex application. But I see what's are you meaning. I shall store the object and then read it again when the application is recreated. – Hunsu Mar 24 '14 at 20:09
  • Exactly :) Glad it helps :) – Nikola Milutinovic Mar 24 '14 at 20:11
0

If you're trying to define a "different layout" to your app when the orientation changes, just define a new layout give it the same name as your other layout and put it under res/layout-land.

So that when your application recreates itself and calls setContentView(layout_name) within the onCreate function it will call the under res/layout-land and not the one under res/layout .

Hosni
  • 668
  • 8
  • 29