PageMaker
PageMaker

Reputation: 395

Lost layout after onConfigurationChanged()

I've been searching quite a while now but still no luck. Changing the code to the now edited version. EDITED On application start with this in onCreate:

    @Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_activity);
    . . . . . 

The app still works flawless. In onConfigurationChange() I removed the setContentView() so the switch actualy does nothing at all:

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
switch(newConfig.orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
    break;
case Configuration.ORIENTATION_LANDSCAPE:
        break;
}
}   //  onConfigurationChanged()  

Now the blanc screens are gone, that's a relieve.
But; When tha app is started in P-mode and configuration changed to L-mode the layout remains the same as in P-mode and visa versa.

I suddenly have a hunch! The activity is only started once! That's exactly what I want, but it means also that the code there is only executed once. I have to check that first.

EDIT

OK, checked out all that was necessary. But the problem remains.

When starting up the app in portrait mode every things is fine. But when I change to landscape mode the configuration change is caried out only the portrait layout is still used and vice versa.

I do have different layouts for different orientations in the xml. I also have

 android:configChanges="orientation|screenSize" >

in the manifest.

I'm out of options, any help will be most appreciated.

EDIT AGAIN

It was hard to proof but I succeded. This is in the XML one for portrait mode one for landscape mode:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/portGame480x800"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="layout_game_activity"
tools:context=".MeMoInfoActivity" >
.....
</LinearLayout



<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/landGame480x800"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
android:tag="layout-land_game_activity"
tools:context=".MeMoInfoActivity" >
....
</LinearLayout>

This the code:

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

Log.i(TAG, "***************************\nMeMoGameActivity onConfigurationChanged\n***************************"); 

switch(newConfig.orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        LinearLayout portLayout = (LinearLayout) findViewById(R.id.portGame480x800);
        if(portLayout != null)
        {
            String portTag = (String) portLayout.getTag().toString();
            Log.i(TAG, "Portrait portTag: "+portTag);
        }
break;
case Configuration.ORIENTATION_LANDSCAPE:
        Toast.makeText(this, "Landscape", Toast.LENGTH_SHORT).show();
        LinearLayout landLayout = (LinearLayout) findViewById(R.id.landGame480x800);
        if(landLayout != null)
        {
            String landTag = (String) landLayout.getTag().toString();

            Log.i(TAG, "LansScape landTag: "+landTag);
        }
        break;
}
}   //  onConfigurationChanged()   

And this is the out from logcat:

09-30 17:10:32.376: I/MeMoGame(2509): ***************************
09-30 17:10:32.376: I/MeMoGame(2509): MeMoGameActivity onConfigurationChanged
09-30 17:10:32.376: I/MeMoGame(2509): ***************************

09-30 17:10:32.534: I/MeMoGame(2509): LANDSCAPE InfoActivity dispWidthPX: 800


09-30 17:11:02.234: I/MeMoGame(2509): ***************************
09-30 17:11:02.234: I/MeMoGame(2509): MeMoGameActivity onConfigurationChanged
09-30 17:11:02.234: I/MeMoGame(2509): ***************************

09-30 17:11:02.384: I/MeMoGame(2509): Portrait portTag: layout_game_activity

09-30 17:11:02.384: I/MeMoGame(2509): PORTRAIT InfoActivity dispWidthPX: 480

09-30 17:11:02.896: D/dalvikvm(2509): GC_CONCURRENT freed 102K, 3% free 6320K/6471K, paused 19ms+7ms, total 320ms

The app was started in portrait mode. It is quit clear that it never executed the landscape layout.

Can / will someone help please?

Upvotes: 1

Views: 430

Answers (1)

fasteque
fasteque

Reputation: 4339

You should let Android framework decide what layout shall be used based on the current configuration. So create 2 folders, layout-land and layout-port, and put your layouts (it's important to use the same filename). Also, remove that switches in the code.

Upvotes: 3

Related Questions