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.

2
  • I thought changing orientation always recreated the activty Commented Mar 24, 2014 at 19:40
  • How do you know that the function is never called? Commented Mar 24, 2014 at 19:41

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

This is a last resource solution and should be avoided as possible
It's worked. but all the tutorials I have read say to just add android:configChanges="orientation|keyboardHidden"
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.'
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 :)

6 Comments

Yes I have read that before. The problem I encountered is that I can't save object (I can but hard to implement).
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.
Yes I need screen rotation
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.
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.
|
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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.