1 page.title=Activity 2 page.tags=Activity 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">Activity</a></li> 15 <li><a href="#RestoreState">Activity</a></li> 16 </ol> 17 18 <h2></h2> 19 <ul> 20 <li><a href="{@docRoot}training/basics/supporting-devices/screens.html"></a> 21 </li> 22 <li><a href="{@docRoot}guide/topics/resources/runtime-changes.html"></a></li> 23 <li><a href="{@docRoot}guide/components/activities.html">Activity</a> 24 </li> 25 </ul> 26 27 </div> 28 </div> 29 30 <p>Activity <em></em>Activity 31 {@link android.app.Activity#finish()} 32 ActivityActivityActivity 33 34 </p> 35 36 <p>Activity<em></em> Activity {@link android.app.Activity} Activity 37 38 Activity 39 {@link android.app.Activity} ActivityActivity 40 41 42 43 {@link android.os.Bundle} 44 </p> 45 46 <p class="caution"><strong></strong>Activity 47 ActivityActivity 48 49 </p> 50 51 <p> {@link android.os.Bundle} Activity {@link android.widget.EditText} {@link android.view.View} 52 53 Activity 54 55 ActivityActivity 56 57 </p> 58 59 <p class="note"><strong></strong> Android Activity<strong></strong> 60 <a href="{@docRoot}reference/android/view/View.html#attr_android:id">{@code 61 android:id}</a> ID 62 </p> 63 64 <p>Activity 65 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} ActivityActivity {@link android.os.Bundle} 66 67 68 Activity {@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>Activity 76 {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} (1) {@link android.app.Activity} Activity (1) {@link android.app.Activity#onCreate onCreate()} (2) {@link android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} (3) 77 78 79 80 81 82 </p> 83 84 85 86 <h2 id="SaveState">Activity</h2> 87 88 <p>Activity {@link android.app.Activity#onSaveInstanceState 89 onSaveInstanceState()} Activity 90 Activity {@link android.widget.EditText} {@link android.widget.ListView} 91 92 </p> 93 94 <p>Activity {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} {@link android.os.Bundle} 95 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">Activity</h2> 121 122 <p>ActivityActivity 123 {@link android.os.Bundle} 124 {@link android.app.Activity#onCreate onCreate()} {@link 125 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} {@link android.os.Bundle} 126 </p> 127 128 <p>Activity {@link android.app.Activity#onCreate onCreate()} {@link android.os.Bundle} null 129 130 nullActivity 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#onStart()} {@link 155 android.app.Activity#onRestoreInstanceState onRestoreInstanceState()} {@link android.app.Activity#onCreate onCreate()} 156 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#onSaveInstanceState onSaveInstanceState()} 174 </p> 175 176 <p>Activity<a href="{@docRoot}guide/topics/resources/runtime-changes.html"></a> 177 </p> 178 179