Activity restart on rotation Android

前端 未结 30 3826
花落未央
花落未央 2020-11-21 04:32

In my Android application, when I rotate the device (slide out the keyboard) then my Activity is restarted (onCreate is called). Now, this is proba

30条回答
  •  情歌与酒
    2020-11-21 05:03

    Use orientation listener to perform different tasks on different orientation.

    @Override
    public void onConfigurationChanged(Configuration myConfig) 
    {
        super.onConfigurationChanged(myConfig);
        int orient = getResources().getConfiguration().orientation; 
        switch(orient) 
        {
           case Configuration.ORIENTATION_LANDSCAPE:
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                        break;
           case Configuration.ORIENTATION_PORTRAIT:
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        break;
           default:
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }
    }
    

提交回复
热议问题