Home | History | Annotate | Download | only in resources
      1 page.title=    
      2 page.tags=, 
      3 @jd:body
      4 
      5 <div id="qv-wrapper">
      6 <div id="qv">
      7 
      8   <h2> </h2>
      9   <ol>
     10     <li><a href="#RetainingAnObject">     </a></li>
     11     <li><a href="#HandlingTheChange">   </a>
     12   </ol>
     13 
     14   <h2>. :</h2>
     15   <ol>
     16     <li><a href="providing-resources.html"> </a></li>
     17     <li><a href="accessing-resources.html">  </a></li>
     18     <li><a href="http://android-developers.blogspot.com/2009/02/faster-screen-orientation-change.html">
     19    </a></li>
     20   </ol>
     21 </div>
     22 </div>
     23 
     24 <p>       
     25 (,  ,    ).    ,
     26 Android  
     27 {@link android.app.Activity} ( {@link android.app.Activity#onDestroy()},  {@link
     28 android.app.Activity#onCreate(Bundle) onCreate()}).    
     29         
     30  ,     .</p>
     31 
     32 <p>    ,    
     33     <a href="{@docRoot}guide/components/activities.html#Lifecycle"> 
     34 </a>,   Android 
     35 {@link android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()}  
     36 ,        .     
     37     {@link android.app.Activity#onCreate(Bundle) onCreate()}  {@link
     38 android.app.Activity#onRestoreInstanceState(Bundle) onRestoreInstanceState()}.</p>
     39 
     40 <p> ,      , 
     41    (,   )    
     42   .        
     43    ,   , ,     
     44          ,  
     45    .  ,    ,   <a href="{@docRoot}guide/components/activities.html#Lifecycle">  </a>.</p>
     46 
     47 <p>  ,     
     48               .   
     49    :</p>
     50 
     51 <ol type="a">
     52   <li><a href="#RetainingAnObject">     </a>
     53   <p>      ,    
     54       .</p>
     55 
     56   </li>
     57   <li><a href="#HandlingTheChange">   </a>
     58   <p>         ,
     59       ,       
     60 .</p>
     61   </li>
     62 </ol>
     63 
     64 
     65 <h2 id="RetainingAnObject">     </h2>
     66 
     67 <p>       ,    
     68     ,      
     69    .  ,       
     70    {@link android.os.Bundle},        {@link
     71 android.app.Activity#onSaveInstanceState(Bundle) onSaveInstanceState()},   
     72      (   ),         
     73 ,   ,        .   
     74           {@link
     75 android.app.Fragment}       .  
     76        ,   .</p>
     77 
     78 <p>  Android     , 
     79 ,   ,  .      
     80      .</p>
     81 
     82 <p>             :</p>
     83 
     84 <ol>
     85   <li>  {@link android.app.Fragment}     ,
     86  .</li>
     87   <li> {@link android.app.Fragment#setRetainInstance(boolean)},   .
     88       </li>
     89   <li>   .</li>
     90   <li> {@link android.app.FragmentManager}     
     91 .</li>
     92 </ol>
     93 
     94 <p>       :</p>
     95 
     96 <pre>
     97 public class RetainedFragment extends Fragment {
     98 
     99     // data object we want to retain
    100     private MyDataObject data;
    101 
    102     // this method is only called once for this fragment
    103     &#64;Override
    104     public void onCreate(Bundle savedInstanceState) {
    105         super.onCreate(savedInstanceState);
    106         // retain this fragment
    107         setRetainInstance(true);
    108     }
    109 
    110     public void setData(MyDataObject data) {
    111         this.data = data;
    112     }
    113 
    114     public MyDataObject getData() {
    115         return data;
    116     }
    117 }
    118 </pre>
    119 
    120 <p class="caution"><strong>!</strong>     ,
    121    ,   {@link android.app.Activity}, , {@link
    122 android.graphics.drawable.Drawable}, {@link android.widget.Adapter}, {@link android.view.View}
    123    ,   {@link android.content.Context}.    
    124        . ( 
    125 ,    ,        , 
    126     ).</p>
    127 
    128 <p>  {@link android.app.FragmentManager}     . 
    129      ,        
    130   .       :</p>
    131 
    132 <pre>
    133 public class MyActivity extends Activity {
    134 
    135     private RetainedFragment dataFragment;
    136 
    137     &#64;Override
    138     public void onCreate(Bundle savedInstanceState) {
    139         super.onCreate(savedInstanceState);
    140         setContentView(R.layout.main);
    141 
    142         // find the retained fragment on activity restarts
    143         FragmentManager fm = getFragmentManager();
    144         dataFragment = (DataFragment) fm.findFragmentByTag(data);
    145 
    146         // create the fragment and data the first time
    147         if (dataFragment == null) {
    148             // add the fragment
    149             dataFragment = new DataFragment();
    150             fm.beginTransaction().add(dataFragment, data).commit();
    151             // load the data from the web
    152             dataFragment.setData(loadMyData());
    153         }
    154 
    155         // the data is available in dataFragment.getData()
    156         ...
    157     }
    158 
    159     &#64;Override
    160     public void onDestroy() {
    161         super.onDestroy();
    162         // store the data in the fragment
    163         dataFragment.setData(collectMyLoadedData());
    164     }
    165 }
    166 </pre>
    167 
    168 <p>   {@link android.app.Activity#onCreate(Bundle) onCreate()}  
    169     .  {@link android.app.Activity#onCreate(Bundle) onCreate()} 
    170  ,  ,   .
    171  {@link android.app.Activity#onDestroy() onDestroy()}  ,  ,  
    172   .</p>
    173 
    174 
    175 
    176 
    177 
    178 <h2 id="HandlingTheChange">   </h2>
    179 
    180 <p>         
    181  <em></em>   ,  
    182  ,       
    183 ,      .</p>
    184 
    185 <p class="note"><strong>.</strong>     
    186     ,    
    187   .       ,      
    188  ,        .</p>
    189 
    190 <p>          <a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code &lt;activity&gt;}</a>
    191         <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code
    192 android:configChanges}</a>  ,  , 
    193   .        <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code
    194 android:configChanges}</a> (    {@code "orientation"} 
    195        {@code "keyboardHidden"}  
    196     ).      
    197  ,      {@code |}.</p>
    198 
    199 <p>,     ,   
    200   ,     :</p>
    201 
    202 <pre>
    203 &lt;activity android:name=".MyActivity"
    204           android:configChanges="orientation|keyboardHidden"
    205           android:label="@string/app_name">
    206 </pre>
    207 
    208 <p>,       ,  {@code MyActivity}  .
    209   {@code MyActivity}   {@link
    210 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}.  
    211   {@link android.content.res.Configuration},   
    212   .    {@link android.content.res.Configuration}
    213        , 
    214 ,    .  
    215     {@link android.content.res.Resources}  
    216       ,   
    217        .</p>
    218 
    219 <p class="caution"><strong>!</strong>   Android 3.2 ( API 13), <strong>
    220    </strong>      
    221  .  ,             
    222  API 13   (    <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a>  <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a>
    223 ),    {@code "screenSize"}     {@code
    224 "orientation"}.     {@code
    225 android:configChanges="orientation|screenSize"}. ,      API
    226 12  ,         ( 
    227           Android 3.2    ).</p>
    228 
    229 <p>,    {@link
    230 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}
    231    :</p>
    232 
    233 <pre>
    234 &#64;Override
    235 public void onConfigurationChanged(Configuration newConfig) {
    236     super.onConfigurationChanged(newConfig);
    237 
    238     // Checks the orientation of the screen
    239     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    240         Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    241     } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    242         Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    243     }
    244 }
    245 </pre>
    246 
    247 <p> {@link android.content.res.Configuration}   
    248 ,    .    ,
    249    ,      ,  
    250   . ,    {@link
    251 android.content.res.Resources} ,  
    252   {@link android.widget.ImageView}   {@link android.widget.ImageView#setImageResource(int)
    253 setImageResource()}
    254 ,      ,    (    <a href="providing-resources.html#AlternateResources"> </a>).</p>
    255 
    256 <p> ,     {@link
    257 android.content.res.Configuration}  ,    
    258   {@link android.content.res.Configuration}.    
    259    .      {@link
    260 android.content.res.Configuration}.</p>
    261 
    262 <p class="note"><strong>!</strong>   ,  
    263 ,      ,     .  
    264 ,     ,     ,       
    265    ,            {@link
    266 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}.</p>
    267 
    268 <p>         
    269 ,    <em></em>   {@link
    270 android.app.Activity#onConfigurationChanged(Configuration) onConfigurationChanged()}.  
    271   ,    ,  
    272 ,     .      
    273       ,      
    274           .   , 
    275    ,     , 
    276   ,     , ,    ,  
    277      .</p>
    278 
    279 <p>     ,     , .   <a href="{@docRoot}guide/topics/manifest/activity-element.html#config">{@code
    280 android:configChanges}</a>  
    281  {@link android.content.res.Configuration}.</p>
    282