why using android:configChanges is a bad practice

后端 未结 3 2097
眼角桃花
眼角桃花 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:47

    To sum up all what i got form @user13 answer and other stackoverflow questions and blog posts i would like to share my finding to clear some very important aspects.

    1. (user13) It's not bad practice to use android:configChanges, but it pretty easily can be, if you don't understand exactly what you're doing

    2. Using this technique prevents you from easily using configuration specific resources. For instance, if you want your layout or drawables or strings or whatever to be different in portrait and landscapes, you have to manage it yourself if you use android:configChanges.

    3. You need to override and use onConfigurationChanged() method to perform specific action if you decide to use android:configChanges
    4. As user13 mentioned Activity is recreated not just due to orientation change but there are multiple reasons due to which activity can be restarted. Therefor activity restart should be handled for all causes. using android:configChanges only handles one case and there will be unhandled cases of activity restart which will cause a potential bug.
    5. There are multiple and better ways to handle activity restart and plenty of help is also available on stactoverflow so android:configChanges should be used as a last resort according to documentation.

提交回复
热议问题