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

前端 未结 5 2053
自闭症患者
自闭症患者 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 03:58

    Your activity is destroyed to give you the opportunity to reconfigure yourself for the new orientation.

    From the developer.android.com:

    When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

    For example, in landscape mode you may require a completely different layout, or may want to load in graphics that would not appear stretched. The best way of doing this is allowing the activity to be created again, which will allow the linking to the layout file to change to a more orientation-friendly layout.

    See http://developer.android.com/training/basics/activity-lifecycle/recreating.html for more info and how to deal with the orientation change

    If you want to disable the recreation you can add

    android:configChanges="orientation"
    

    to your Activity element in AndroidManifest.xml. This way your Activity will not be reloaded.

    onSaveInstance and onRestoreInstace should only be used for passing through session information, for example the current text in a TextField, and nothing generic that can just be loaded in again after onCreate.

提交回复
热议问题