1 page.title= 2 page.tags=activity lifecycle 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> . 31 <em></em> {@link android.app.Activity#finish()} 32 . 33 , 34 .</p> 35 36 <p> <em></em> 37 , {@link android.app.Activity} . 38 . 39 , 40 {@link android.app.Activity} . 41 , 42 . 43 " " , {@link android.os.Bundle} 44 - .</p> 45 46 <p class="caution"><strong>:</strong> 47 . 48 . , 49 .</p> 50 51 <p> {@link android.os.Bundle} {@link android.view.View} 52 (: {@link android.widget.EditText} 53 ). 54 , 55 . 56 57 .</p> 58 59 <p class="note"><strong>:</strong> Android 60 61 <a href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code 62 android:id}</a> <strong> ID </strong>.</p> 63 64 <p> 65 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} . 66 , 67 68 {@link android.os.Bundle} . 69 , {@link 70 android.os.Bundle} {@link android.app.Activity#onRestoreInstanceState 71 onRestoreInstanceState()} {@link android.app.Activity#onCreate onCreate()} 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) . 77 {@link android.app.Activity} 78 . 79 , {@link android.app.Activity#onCreate onCreate()} (2) 80 {@link android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} (3) 81 (1) 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> {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} 95 {@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 @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> , 123 {@link android.os.Bundle} 124 . {@link android.app.Activity#onCreate onCreate()} {@link 125 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} 126 {@link android.os.Bundle} .</p> 127 128 <p>{@link android.app.Activity#onCreate onCreate()} 129 , , {@link android.os.Bundle} null 130 . null , 131 132 .</p> 133 134 <p> {@link android.app.Activity#onCreate 135 onCreate()} .</p> 136 137 <pre> 138 @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()} 156 {@link 157 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} . 158 {@link 159 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} . {@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#onSaveInstanceState onSaveInstanceState()} 174 .</p> 175 176 <p> 177 <a href="{@docRoot}guide/topics/resources/runtime-changes.html"> </a> .</p> 178 179