Question

I'm using api 2.3.3 in a new project, I use my tablet samsung galaxy tab 2 and when I change the orientation creates a new activity.

This is the simple code I'm trying:

MAIN ACTIVITY

public class MainActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.e("TAG","ONCREATE");
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
                Log.e("TAG","LANDSCAPE");
            }else{
                Log.e("TAG","PORTRAIT");
            }
        }

MANIFEST

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:configChanges="orientation|keyboardHidden|keyboard"> 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />            
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

I think that my code is well, I have read many examples and it is the same.

Whats happening me?

Thanks

Était-ce utile?

La solution

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) 
{
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null) 
    {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            // code for portrait
        }
        else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            // code for landscape
        }

    }
}

try the above code instead of onConfigurationChanged method , it worked good for me .

Autres conseils

If you are building for an API that is greater than 13, then you need to use:

android:configChanges="orientation|screenSize"

try to remove keyboard & keyboardHidden word in manifest file and check it. like:

android:configChanges="orientation"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top