Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android activity restarted when orientation changes

I read many posts about this problem like [this link][1] and one solution is add orientation configChanges to manifest and handle onConfigurationChanged event in order to prevent onCreate activity to be called again when rotation. I did it and event is triggered properly, however, after this execution, onCreate method is also executed! why? what I am missing? Thank you

manifest,

<activity 
            android:name="webPush"
            android:configChanges="keyboardHidden|orientation"/>

activity,

@Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.vistaaib);
    }

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vistaaib);
...
like image 349
Jaume Avatar asked Jul 01 '12 08:07

Jaume


2 Answers

I think this will work.........

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

Beginning with Android 3.2 (API level 13), the will "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher you must use

android:configChanges="orientation|screenSize"
like image 154
Kumar Vivek Mitra Avatar answered Sep 30 '22 19:09

Kumar Vivek Mitra


I Did this. I added This code to manifest and it works perfect.

<activity
        android:name="?"
        android:label="@string/?"
        android:theme="@style/?" 
        android:configChanges="orientation|screenSize">

You will need to add this under you activity if you want to change something when the device is rotation.

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
like image 23
Thor1401 Avatar answered Sep 30 '22 19:09

Thor1401