Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:android1 [2014/02/08 19:17] alfred [Applications] |
fw:android1 [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 42: | Línea 42: | ||
| }); | }); | ||
| </code> | </code> | ||
| - | === Activity lifecicle === | + | |
| + | === Recover the activity status === | ||
| + | When an Activity is finished it can be restored using a Bundle object, it stores key-value pairs. To save additional data about the activity state, you must override the onSaveInstanceState() callback method. Then the system will pass the same Bundle object to both the onRestoreInstanceState() and onCreate() methods. | ||
| + | <code java> | ||
| + | static final String STATE_SCORE = "playerScore"; | ||
| + | static final String STATE_LEVEL = "playerLevel"; | ||
| + | ... | ||
| + | @Override | ||
| + | public void onSaveInstanceState(Bundle savedInstanceState) { | ||
| + | savedInstanceState.putInt(STATE_SCORE, mCurrentScore); | ||
| + | savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); | ||
| + | super.onSaveInstanceState(savedInstanceState); | ||
| + | } | ||
| + | </code> | ||
| + | <code java> | ||
| + | protected void onCreate(Bundle savedInstanceState) { | ||
| + | super.onCreate(savedInstanceState); // Always call the superclass first | ||
| + | if (savedInstanceState != null) { | ||
| + | mCurrentScore = savedInstanceState.getInt(STATE_SCORE); | ||
| + | mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); | ||
| + | } else { | ||
| + | // Probably initialize members with default values for a new instance | ||
| + | } | ||
| + | </code> | ||
| + | You may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null: | ||
| + | <code java> | ||
| + | public void onRestoreInstanceState(Bundle savedInstanceState) { | ||
| + | // Always call the superclass so it can restore the view hierarchy | ||
| + | super.onRestoreInstanceState(savedInstanceState); | ||
| + | |||
| + | // Restore state members from saved instance | ||
| + | mCurrentScore = savedInstanceState.getInt(STATE_SCORE); | ||
| + | mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); | ||
| + | } | ||
| + | </code> | ||
| + | ==== Activity lifecicle ==== | ||
| {{ :fw:android:az0jw.png?300 |}} | {{ :fw:android:az0jw.png?300 |}} | ||
| The activity will be activated between the ''onStart'' and the ''onStop'' steps. \\ | The activity will be activated between the ''onStart'' and the ''onStop'' steps. \\ | ||