why using android:configChanges is a bad practice

后端 未结 3 2168
眼角桃花
眼角桃花 2021-02-19 21:23

I have gone through different posts and questions for handling rotation and AsyncTask. In each post it is mentioned that using android:configChanges is

3条回答
  •  遇见更好的自我
    2021-02-19 21:38

    Using android:configChanges is good practice if you know what you are doing.

    Just always test how your application how it behaves when it is restarted by system to stay comfortable for user so some state has to be saved all the time but not all. With config changes like this:

    android:configChanges="locale|keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

    Your application will restart rather rarely on new devices that have a lot of memory. If it restarts it's not so unexpected for user anyway, as user had attention elsewhere and came back to app. User don't have to be in exact same state after restart if it happens by manual killing of application or application restart because of some other heavy tasks (playing game) user is doing, the user experience is important here.

    If you need to refresh List just for different layouts for orientations changes or you need to hide some view elements you can call:

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        _list.reloadData();
        _editorButton.visible(isPortrait());
    }
    

    (I use my custom classes but you get the point)

提交回复
热议问题