Change screen orientation in Android without reloading the activity

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 02:51:57

See the edit by @luisfer. For targeting Android 3.2 and above, you need BOTH

android:configChanges="orientation|screenSize"

http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter

In AndroidManifest file add android:configChanges="orientation" for the activity you want to handle this orientation

In activity use onConfigurationChange overrided method. Do task you want to handle in orientation change.

ashah

Call this method And Set manifest file

android:configChanges="orientation|screenSize|keyboardHidden"

public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!