What are the stages of the life cycle of an activity and fragment?

0

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?

    
asked by anonymous 02.10.2014 / 01:12

2 answers

5

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;
}

}

    
02.10.2014 / 16:51
5

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:

  • Android will call the methods: onPause () and onStop ().
  • If the user returns to your app then the nRestart (), onStart () and onResume () and your app will start working again.

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?

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

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();
}

link

    
06.11.2016 / 16:59