What are the life cycle stages of an Activity and Fragment?

Asked

Viewed 3,914 times

0

What are the life cycle stages of an Activity and Fragment? What are the differences of these stages? And how can I identify at which stage an application’s Activity or Fragment is currently?

2 answers

6

inserir a descrição da imagem aqui

Operation of each method of an Activity:

onCreate() - It is the first function to be executed in an Activity. It is usually responsible for loading XML layouts and other boot operations. Is executed only once.

onStart() - It is called immediately after onCreate() - and also when a background activity is back in focus.

onResume() - Just like onStart(), it is called on the Activity startup and also when an Activity has focus again. What is the difference between the two? onStart() is only called when Activity is no longer visible and re-focuses, onResume() is called in the "focus resumes".

onPause() - It is the first function to be invoked when Activity loses focus (this occurs when a new Activity is started) .

onStop() - It is only called when Activity is completely covered up by another Activity.

onDestroy() - The last function to be executed. After it, Activity is considered "dead" - that is, it can no longer be relaunched. If the user requests this Activity again, a new object will be built.

onRestart() - Call immediately before onStart(), when an Activity again focuses after being in the background.


Now let’s imagine that the user was browsing in your application and suddenly clicked on the HOME button what happens in your app:

  • Android will call the methods: onPause() and onStop().
  • If the user goes back to your app then thenRestart(), onStart() and onResume() and your app will run again from there.

Now let’s imagine that the user is inside your application and click on a button calling another Activity. He browses the new Activity and suddenly he clicks the Back button what happens to our Activities ?

  • Android will call the method onPause() of the second Activity thus pausing any interaction.
  • Will be called the methods: onRestart(), onStart() and onResume() of the previous Activity.
  • If everything goes well then android calls the methods onStop() and onDestroy() from the second Activity thus killing the second screen and showing the first screen beautiful and ready to work.

To identify the steps, you can do as follows: Right Button>Generate>Override Methods(Override Methods), and add the methods onCreate, onStart, onStop, onDestroy, on Restart... The following commands will be added in Activity.java for each selected method:

    protected void onStart() 
   {
     super.onStart();
   }
    protected void onRestart() 
   {
    super.onRestart();
   }

...and so for all methods. To identify the step you are on, you can add a message on the screen in each method, with the description of the step, for example, in onStart’s slideshow add: Toast.makeText(getApplicationContext(),"onStart Metodo Chamado", Toast.LENGTH_SHORT).show(); Being as follows:

protected void onStart() 
{
    super.onStart();
    Toast.makeText(getApplicationContext(),"onStart Metodo Chamado", Toast.LENGTH_SHORT).show();
}

http://www.devmedia.com.br/entendendo-o-ciclo-de-vida-de-uma-aplicacao-android/22922

5


In itself Google development website has this information, despite being in English has a diagram very easy to understand.

Activity

And regarding how to identify in which step an Activity is you will need to implement a code that overwrites each Activity lifecycle method to update the status:

import android.app.Activity;
import android.os.Bundle;

public class MinhaActivity extends Activity {
private CicloDeVida cicloDeVida;

public enum CicloDeVida
{
    onCreate,
    onStart,
    onResume,
    onRestart,
    onPause,
    onStop,
    onDestroy
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    cicloDeVida = CicloDeVida.onCreate;
}

@Override
protected void onStart()
{
    super.onStart();

    cicloDeVida = CicloDeVida.onStart;
}

@Override
protected void onResume()
{
    super.onResume();

    cicloDeVida = CicloDeVida.onResume;
}

@Override
protected void onRestart()
{
    super.onRestart();

    cicloDeVida = CicloDeVida.onRestart;
}

@Override
protected void onPause()
{
    super.onPause();

    cicloDeVida = CicloDeVida.onPause;
}

@Override
protected void onStop()
{
    super.onStop();

    cicloDeVida = CicloDeVida.onStop;
}

@Override
protected void onDestroy()
{
    super.onDestroy();

    cicloDeVida = CicloDeVida.onDestroy;
}

}

  • I wanted an explanation from you... not a copy and glue.. and it is still in English... and did not present my other question "And how can I identify at which stage an application Activity or Fragment is at the moment?"

  • I edited it, see if it answers your question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.