Home | History | Annotate | Download | only in activity-lifecycle
      1 page.title=
      2 page.tags=
      3 helpoutsWidget=true
      4 
      5 trainingnavtop=true
      6 
      7 @jd:body
      8 
      9 <div id="tb-wrapper">
     10   <div id="tb">
     11     
     12     <h2></h2>
     13     <ol>
     14       <li><a href="#SaveState"></a></li>
     15       <li><a href="#RestoreState"></a></li>
     16     </ol>
     17     
     18     <h2></h2>
     19     <ul>
     20       <li><a href="{@docRoot}training/basics/supporting-devices/screens.html">
     21 </a></li>
     22       <li><a href="{@docRoot}guide/topics/resources/runtime-changes.html"></a></li>
     23       <li><a href="{@docRoot}guide/components/activities.html"></a>
     24       </li>
     25     </ul>
     26 
     27   </div>
     28 </div>
     29 
     30 <p> <em>[]</em>  {@link android.app.Activity#finish()} 
     31 
     32  
     33 
     34 </p>
     35 
     36 <p> <em>[]</em>  {@link android.app.Activity} 
     37 
     38 
     39 
     40 {@link android.app.Activity} 
     41 
     42 {@link android.os.Bundle} 
     43 
     44 </p>
     45 
     46 <p class="caution"><strong>: </strong>
     47 
     48 
     49 </p>
     50 
     51 <p> {@link android.os.Bundle}  {@link android.view.View} {@link android.widget.EditText} 
     52 
     53 
     54 
     55 
     56 
     57 </p>
     58 
     59 <p class="note"><strong>:</strong> Android <strong> ID </strong>
     60  <a href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code
     61 android:id}</a> 
     62 </p>
     63 
     64 <p>
     65 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()}  {@link android.os.Bundle} 
     66 
     67 
     68  {@link
     69 android.os.Bundle}  {@link android.app.Activity#onRestoreInstanceState
     70 onRestoreInstanceState()}  {@link android.app.Activity#onCreate onCreate()}
     71  
     72 </p>
     73 
     74 <img src="{@docRoot}images/training/basics/basic-lifecycle-savestate.png" />
     75 <p class="img-caption"><strong> 2.</strong> 
     76 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()}1{@link android.app.Activity} 1 {@link android.app.Activity#onCreate onCreate()} 2 
     77 {@link android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} 3
     78 
     79 
     80 
     81 
     82 </p>
     83 
     84 
     85 
     86 <h2 id="SaveState"></h2>
     87 
     88 <p> {@link android.app.Activity#onSaveInstanceState
     89 onSaveInstanceState()} 
     90 {@link android.widget.EditText}  {@link android.widget.ListView} 
     91 
     92 </p>
     93 
     94 <p>
     95 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} {@link android.os.Bundle} 
     96 </p>
     97 
     98 <pre>
     99 static final String STATE_SCORE = "playerScore";
    100 static final String STATE_LEVEL = "playerLevel";
    101 ...
    102 
    103 &#64;Override
    104 public void onSaveInstanceState(Bundle savedInstanceState) {
    105     // Save the user's current game state
    106     savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    107     savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
    108     
    109     // Always call the superclass so it can save the view hierarchy state
    110     super.onSaveInstanceState(savedInstanceState);
    111 }
    112 </pre>
    113 
    114 <p class="caution"><strong>: </strong>{@link
    115 android.app.Activity#onSaveInstanceState onSaveInstanceState()} 
    116 </p>
    117 
    118 
    119 
    120 <h2 id="RestoreState"></h2>
    121 
    122 <p> {@link android.os.Bundle} 
    123 
    124 {@link android.app.Activity#onCreate onCreate()}  {@link
    125 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()}   {@link android.os.Bundle} 
    126 </p>
    127 
    128 <p>{@link android.app.Activity#onCreate onCreate()}  {@link android.os.Bundle}  null 
    129 
    130 null 
    131 
    132 </p>
    133 
    134 <p>{@link android.app.Activity#onCreate
    135 onCreate()} </p>
    136 
    137 <pre>
    138 &#64;Override
    139 protected void onCreate(Bundle savedInstanceState) {
    140     super.onCreate(savedInstanceState); // Always call the superclass first
    141    
    142     // Check whether we're recreating a previously destroyed instance
    143     if (savedInstanceState != null) {
    144         // Restore value of members from saved state
    145         mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    146         mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    147     } else {
    148         // Probably initialize members with default values for a new instance
    149     }
    150     ...
    151 }
    152 </pre>
    153 
    154 <p>{@link android.app.Activity#onCreate onCreate()} 
    155 {@link android.app.Activity#onStart()}  {@link
    156 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} 
    157  {@link
    158 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} 
    159 {@link android.os.Bundle}  null </p>
    160         
    161 <pre>
    162 public void onRestoreInstanceState(Bundle savedInstanceState) {
    163     // Always call the superclass so it can restore the view hierarchy
    164     super.onRestoreInstanceState(savedInstanceState);
    165    
    166     // Restore state members from saved instance
    167     mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    168     mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    169 }
    170 </pre>
    171 
    172 <p class="caution"><strong>: </strong>{@link
    173 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} 
    174 </p>
    175 
    176 <p><a href="{@docRoot}guide/topics/resources/runtime-changes.html"></a>
    177 </p>
    178 
    179