What is the advantage of letting an activity be destroyed on rotation?

前端 未结 5 2052
自闭症患者
自闭症患者 2021-01-01 03:27

I have used both approaches:

  1. Let the activity be destroyed on rotation
  2. Don\'t let the activity be destroyed on rotation

My approach alm

5条回答
  •  悲哀的现实
    2021-01-01 04:11

    Am I missing something?

    Yes. You are assuming that your alternative is somehow less error prone.

    By not going through the destroy-and-recreate cycle, you have to ensure that you are handling changing every resource for every possible configuration change.

    Don't let the activity be destroyed on rotation

    Unless you are using android:screenOrientation to force your activity into a single orientation (e.g., landscape), you cannot only handle rotation-related configuration changes. You need to handle all configuration changes. Otherwise, as soon as the user drops their device into a dock, removes it from a dock, changes language from Settings, attaches or detaches a keyboard, changes the global font scaling, etc., your app will break.

    This, in turn, means that on every configuration change, you need to:

    • update your UI for your potentially new string resources
    • adjust or reload your layouts (and by "adjust" that includes changing any drawables, animations, menus, etc.)
    • anything else tied to your resources (e.g., array lists in your PreferenceFragment)

    The problem is that you are going to forget something. For example, you will miss changing a string associated with an action bar item, so now most of your UI is in Spanish and that action bar item is in English. The sorts of things you are going to forget will be less obvious (how often do you test your Spanish translation?).

提交回复
热议问题