开发者

onConfigurationChanged-it's still showing the first layout

I have an activity which handles configuration changes. But now i have to change layout.I tried in onConfigurationChanged callback just to set again the layout and hoping will get the correct layout(land) but it's still showing the first layout for portrait view ( there two l开发者_StackOverflow中文版ayout(same name) are placed in res/ layout and res/layout-land :)

if I delete android:configChanges="orientation", it works should be, but ı need to handle onConfigurationChanged. What should I do??


If you have your portrait layout main.xml in /res/layout and your landscape layout main.xml in /res/layout-land, and your onConfigurationChanged() looks like this:

    public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);                
            setContentView(R.layout.main);
    ...
    ...
    ...

    }

And in your Manifest you should have android:configChanges="keyboardHidden|orientation"

Then it should work fine, as this does in my app. Is this what you are doing?


// In your activity code.
int mCurrentOrientation;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mfo_offers);
    ........
    mCurrentOrientation = getCurrentOrientation();
}
@Override    
public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // You may handle different configuration changes in your activity which configured in your mainfest.xml, you only need to recreate layout when orientation changes.
        if(mCurrentOrientation != newConfig.orientation) {          
            recreate(); // This recreate the activity if you can properly restore your activity state.  
        }
        ......
}

See Activity.recreate() here: http://developer.android.com/reference/android/app/Activity.html#recreate()

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜