5

I have a activity in which there is a spinner. since for portrait and landscape mode i have different layout so I am changing layout in onConfigurationChanged method

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

but the problem is when I change orientation , my spinner is recreated so if spinner is open in portrait mode it get close in landscape mode.My requirement is : if it is open in any mode , it should be open after orientation change.can you please let me know how to handle this situation.

  • I have not tried but there are two method osavestate and restore something like that in spinner.Once try them – Tofeeq Ahmad Jan 12 '12 at 05:14
  • I think you will get the solution of your problem from here..... http://developer.android.com/resources/articles/faster-screen-orientation-change.html – himanshu Jan 12 '12 at 06:46

3 Answers3

0

try spinner's performClick() method

Jiang Qi
  • 4,450
  • 3
  • 19
  • 19
-1

To stop re-creation of your Spinner you can add this in your Manifest file

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

But by adding this your Layout will not be changed automatically when you rotate your device, so you have to manage that manually like this,

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }

For more information you can check my answer here.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
-1

Once the orientation changes, destroy method get called and your activity re-create once again. To avoid destroy method get called you need to add below codes in manifest file. But in this case only one layout can be used, if you want to repostion your contents you need to do it dynamically then.

android:configChanges="orientation|keyboardHidden"
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37