开发者

problem in rotation in android

开发者 https://www.devze.com 2023-03-06 11:55 出处:网络
hi all i hav a lil problem. actually i have two layouts layout-port and layout-land. and 2 screens. both screens are in both modes of layout.

hi all i hav a lil problem.

actually i have two layouts layout-port and layout-land.

and 2 screens. both screens are in both modes of layout.

1) First Screen is Login Password Screen

2) Second Screen is List开发者_运维技巧View Screen

Now whenever i rotate my cell phone. it recreates the onCreate() method. and then change the layout as per required.

Now due to that recalling of onCreate() my list data is regenerated. How to overcome this problem. Please Tell me..

i even tried writing the following code in the menifest file

android:configChanges="orientation|keyboard|keyboardHidden"

This code works in a sense that it does not recall the onCreate() method but now the problem is that, it also does not change the layout automatically. Mean if i m in portrait then rotate it to landscape it will not change the Layout mode. Please guide what can i do with it.

Thanks a bunch in advance


Save some value in onRetainNonConfigurationInstance method
so you can check getLastNonConfigurationInstance() is null, If null only do your list data generate code

i.e in you onCreate do like this

final Object data = getLastNonConfigurationInstance();

    // The activity is starting for the first time
    if (data == null) {
        //code to generate list data 
    } else {
        // The activity was destroyed/created automatically

    }

Refer this Faster Screen Orientation Change,


Its just a hack to your problem instead of putting your xml in different folder put them in layout folder only and make their names different, now in code

@Override
 public void onConfigurationChanged(Configuration newConfig)
 {
     super.onConfigurationChanged(newConfig);
             if(getScreenOrientation() == 2)
                  setContentView(landscape.xml);
             else
                  setContentView(portrait.xml);
 }

private int getScreenOrientation()
{
    return getResources().getConfiguration().orientation;
}

and in manifest

android:configChanges="orientation|keyboard|keyboardHidden"
0

精彩评论

暂无评论...
验证码 换一张
取 消