简体   繁体   中英

Android: start Activity always in portrait but recognize orientation change

I want to start a new activity/intent always in portrait orientation (even if the device is held in landscape)

If I use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); the Activity is locked to portrait, but I want to recognize orientational changes.

So I think I have to call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); on the first start, when the activity is created, an need an listener to remove this restriction.

But in which methods should I do that? In the constructor , or in onCreate() or elsewhere???

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);

        System.out.println("on config change method called.");

        if(Configuration.ORIENTATION_LANDSCAPE==newConfig.orientation)
        {

                    //Portatrate to landscape...
            System.out.println("on config change method called portraite mode.");
        }
        else if(Configuration.ORIENTATION_PORTRAIT==newConfig.orientation)
        {
            //Landscape to portraite....
            System.out.println("on config change method called landscape mode.");
        }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM