What are the steps in the lifecycle of an activity and fragment? What are the differences between these steps? And how can I identify in which step an activity or fragment of an application is currently in the process?
What are the steps in the lifecycle of an activity and fragment? What are the differences between these steps? And how can I identify in which step an activity or fragment of an application is currently in the process?
In the Google development site has this information, even though it's in English has a diagram very easy to understand.
Activity
And regarding how to identify what stage an Activity is in you should implement a code that overrides 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;
}
}
FunctionofeachmethodofanActivity:
onCreate()-ThisisthefirstfunctiontoexecuteonanActivity.ItisusuallyresponsibleforloadingXMLlayoutsandotherinitializationoperations.Itrunsonlyonce.
onStart()-ItiscalledimmediatelyaftertheonCreate()-andalsowhenanActivitythatwasinbackgroundreturnstofocus.
onResume()-JustlikeonStart(),itiscalledatActivityinitializationandalsowhenanActivityreturnstofocus.What'sthedifferencebetweenthetwo?OnStart()isonlycalledwhenActivitywasnolongervisibleandresumesfocus,onResume()iscalledin"focus resumes". onPause () - This is the first function to invoke when the Activity loses focus (this occurs when a new Activity is started).
onStop () - Only called when Activity is completely covered by another Activity.
onDestroy () - The last function to execute. After it, Activity is considered "dead" - meaning it can not be re-released. If the user re-requests this Activity, a new object will be constructed.
onRestart () - Call immediately before onStart (), when an Activity returns to focus once it is in the background.
Now let's imagine that the user was browsing your app and suddenly clicked the HOME button what happens in your app:
Now let's imagine that the user is inside your application and clicks a button by calling another Activity. He navigates the new Activity and suddenly he clicks the Back button what happens to our Activities?
To identify the steps, you can do the following: Right-click> Generate> Override Methods, and add the methods onCreate, onStart, onStop, onDestroy, on Restart ... Will be added in Activity.java the following commands 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 in, you can add a message on the screen in each method, with the description of the step, for example, in the onStart meotodo add: Toast.makeText(getApplicationContext(),"onStart Metodo Chamado", Toast.LENGTH_SHORT).show();
It looks like this:
protected void onStart()
{
super.onStart();
Toast.makeText(getApplicationContext(),"onStart Metodo Chamado", Toast.LENGTH_SHORT).show();
}