Android: screen rotation, on destroy and services connundrum

让人想犯罪 __ 提交于 2019-12-02 01:01:22

If you add "android:configChanges="orientation"" into your Manifest to prevent the activity from being destroyed and re-created, you might want to implement the method:

public void onConfigurationChanged(Configuration newConfig)

This method is executed every time the system configuration is changed, i.e. when you rotate the phone and orientation is changed. Inside this method you can re-apply a new layout for your activity:

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

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Log.e(TAG, "ORIENTATION_LANDSCAPE");
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Log.e(TAG, "ORIENTATION_PORTRAIT");
    }
}
sbsatter

to add to @alex.veprix to the point (and correct I must mention) answer, if you're building for devices with API level 13 or higher, don't forget to add ScreenSize attribute in the following way: android:configChanges="orientation|screenSize"

Details here: http://developer.android.com/guide/topics/manifest/activity-element.html

The recommended way to do this is to have a TaskFragment handling your logic, and then some callbacks to your activity to sync with your UI.

See the following site for more detailed explanation: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!