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/03 23:16] alfred [Applications] |
fw:android1 [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 20: | Línea 20: | ||
| - You'll pass this Intent to the methods ''startActivity'' or ''startActivityForResult''. | - You'll pass this Intent to the methods ''startActivity'' or ''startActivityForResult''. | ||
| - | === Activity lifecicle === | + | === Create Activity === |
| + | <code java> | ||
| + | Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); | ||
| + | launchActivityTwoButton.setOnClickListener(new OnClickListener() { | ||
| + | @Override | ||
| + | public void onClick(View v) { | ||
| + | Intent activity2 = new Intent(ActivityOne.this, ActivityTwo.class); | ||
| + | startActivity(activity2); | ||
| + | } | ||
| + | }); | ||
| + | </code> | ||
| + | |||
| + | === Finish Activity === | ||
| + | <code java> | ||
| + | Button closeButton = (Button) findViewById(R.id.bClose); | ||
| + | closeButton.setOnClickListener(new OnClickListener() { | ||
| + | @Override | ||
| + | public void onClick(View v) { | ||
| + | ActivityTwo.this.finish(); | ||
| + | } | ||
| + | }); | ||
| + | </code> | ||
| + | |||
| + | === 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. \\ | ||
| Línea 28: | Línea 85: | ||
| === Tasks === | === Tasks === | ||
| A task is a set of related Activities, they don't have to be part of the same application. A task could be the contacts screen, which can be called from single activities. | A task is a set of related Activities, they don't have to be part of the same application. A task could be the contacts screen, which can be called from single activities. | ||
| + | |||
| + | === Configuration changes === | ||
| + | Detection of configuration changes like change the mobile orientation from portrait to landscape. | ||
| ==== Resources ==== | ==== Resources ==== | ||
| === R class === | === R class === | ||
| Línea 54: | Línea 114: | ||
| </code> | </code> | ||
| + | ==== Controls ==== | ||
| + | You can have a TextView in your layout and want to access it by code... | ||
| + | <code java> | ||
| + | protected void onCreate(Bundle savedInstanceState) { | ||
| + | super.onCreate(savedInstanceState); | ||
| + | setContentView(R.layout.activity_one); | ||
| + | mTvCreate = (TextView)findViewById(R.id.create); | ||
| + | ... | ||
| + | </code> | ||
| ==== AndroidManifest.xml ==== | ==== AndroidManifest.xml ==== | ||
| It's a file that defines the developed application. It includes... | It's a file that defines the developed application. It includes... | ||