Android Tutorial - LifeCycle of Activities

Hi everyone again to my blog!
Today I'm gonna show you a quick tutorial about managing the lifecycle of activities on android.

First of all, here is a short explanation of android activities states and a simple diagram about them:

  • Initially, the activity will be created, in order to do that onCreate() method is called.
  • Then the activity is considered "Created" and proceed to be started (with the method onStart()) and subsequently is resumed (by onResume()). At this point the activity is running and processing whatever is programmed to process. Those previous method can be overrided, generally to recover a state or information that were saved in the past.
  • Afterwards activity can be paused (with metod onPause()). So It's not running and is not visible until the system call onResume(). It's also necesary to know that at that point "Android" can destroy the process (calling onStop  and later onDestroy()). For that reason, onPause() and onStop() are usually used to save information so that the process can be restored at the same point.



Now, Create a new Android-Project by clicking File>New>Other  and there Android Application Project. Name it what you want, it's not an important issue.

Then open the file "MainActivity.java" on your "src folder". Click right on the editor and select Source>Override/Implement Methods. In the new dialog we are able to select which function we want to change. For this tutorial, select: "onDestroy, onPause, onRestart, onResume, onStart y onStop"

The only action that our new application is gonna do is to show a "Toast" message when the activity's state changes. To do that we only need to add this sentences in every method only changing the showing text:

Toast.makeText(getApplicationContext(), "Starting activity",Toast.LENGTH_SHORT).show();

Here is the complete code:


public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 protected void onDestroy() {
  Toast.makeText(getApplicationContext(), "Destroying activity",
    Toast.LENGTH_SHORT).show();
  super.onDestroy();
 }

 @Override
 protected void onPause() {
  Toast.makeText(getApplicationContext(), "Pausing activity",
    Toast.LENGTH_SHORT).show();
  super.onPause();
 }

 @Override
 protected void onRestart() {
  Toast.makeText(getApplicationContext(), "Restarting activity",
    Toast.LENGTH_SHORT).show();
  super.onRestart();
 }

 @Override
 protected void onResume() {
  Toast.makeText(getApplicationContext(), "Resuming activity",
    Toast.LENGTH_SHORT).show();
  super.onResume();
 }

 @Override
 protected void onStart() {
  Toast.makeText(getApplicationContext(), "Starting activity",
    Toast.LENGTH_SHORT).show();
  super.onStart();
 }

 @Override
 protected void onStop() {
  Toast.makeText(getApplicationContext(), "Stoping activity",
    Toast.LENGTH_SHORT).show();
  super.onStop();
 }

}


At last but not at least, install the app on your mobile phone and try on. Pay attention to the messages, it's not  the same leave the app clicking home button that return button (The first one only pause the application, but the second one can close it completely)


So, that's all again!
Thanks for reading and see you soon---!




No hay comentarios:

Publicar un comentario