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> , . , , 31 <em></em> 32 {@link android.app.Activity#finish()}. , 33 , 34 .</p> 35 36 <p> <em></em> 37 , , {@link android.app.Activity} , 38 , . 39 ( ), 40 {@link android.app.Activity} , , 41 , , 42 , . , 43 , " " 44 "-", {@link android.os.Bundle}.</p> 45 46 <p class="caution"><strong>!</strong> , 47 . 48 , 49 ( ).</p> 50 51 <p> {@link android.os.Bundle} 52 {@link android.view.View} (, , 53 {@link android.widget.EditText}). , 54 , , 55 . 56 , , , 57 .</p> 58 59 <p class="note"><strong>.</strong> Android 60 , <strong> </strong>, 61 <a href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code 62 android:id}</a>.</p> 63 64 <p> , 65 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()}. 66 , , 67 {@link android.os.Bundle}, 68 , . 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 , 80 , (1), {@link android.app.Activity#onCreate onCreate()} 81 (2) {@link android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} 82 (3).</p> 83 84 85 86 <h2 id="SaveState"> </h2> 87 88 <p> , {@link android.app.Activity#onSaveInstanceState 89 onSaveInstanceState()}, 90 "-". 91 , {@link android.widget.EditText} 92 {@link android.widget.ListView}.</p> 93 94 <p> 95 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} 96 {@link android.os.Bundle} "-". :</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 , , 130 {@link android.os.Bundle} 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 156 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()}, 157 {@link android.app.Activity#onStart()}. {@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> 177 ( ) <a href="{@docRoot}guide/topics/resources/runtime-changes.html"> </a>.</p> 178 179