En Android , una actividad se conoce como una pantalla en una aplicación. Es muy similar a una ventana única de cualquier aplicación de escritorio. Una aplicación de Android consta de una o más pantallas o actividades.
Cada actividad pasa por varias etapas o un ciclo de vida y es administrada por pilas de actividades. Así, cuando comienza una nueva actividad, la anterior siempre permanece debajo de ella. Hay cuatro etapas de una actividad.
- Si una actividad está en primer plano de la pantalla, es decir, en la parte superior de la pila, se dice que está activa o en ejecución. Esta suele ser la actividad con la que el usuario está interactuando actualmente.
- Si una actividad ha perdido el foco y una actividad transparente o de tamaño no completo se ha centrado en la parte superior de su actividad. En tal caso, otra actividad tiene una posición más alta en el modo de ventanas múltiples o la actividad en sí no se puede enfocar en el modo de ventana actual. Tal actividad está completamente viva.
- Si una actividad está completamente oculta por otra actividad, se detiene u oculta. Todavía retiene toda la información, y como su ventana está oculta, a menudo el sistema la eliminará cuando se necesite memoria en otro lugar.
- El sistema puede destruir la actividad de la memoria pidiéndole que finalice o simplemente eliminando su proceso. Cuando se vuelve a mostrar al usuario, debe reiniciarse por completo y restaurarse a su estado anterior.
Para cada etapa, Android nos proporciona un conjunto de 7 métodos que tienen su propio significado para cada etapa del ciclo de vida. La imagen muestra una ruta de migración cada vez que una aplicación cambia de un estado a otro.
Java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Bundle containing previous frozen state setContentView(R.layout.activity_main); // The content view pointing to the id of layout // in the file activity_main.xml Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show(); } }
Kotlin
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Bundle containing previous frozen state setContentView(R.layout.activity_main) // The content view pointing to the id of layout // in the file activity_main.xml val toast = Toast.makeText(applicationContext, "onCreate Called", Toast.LENGTH_LONG).show() } }
Java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Bundle containing previous frozen state setContentView(R.layout.activity_main); // The content view pointing to the id of layout // in the file activity_main.xml Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show(); } protected void onStart() { // It will show a message on the screen // then onStart is invoked Toast toast = Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_LONG).show(); } }
Kotlin
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toast = Toast.makeText(applicationContext, "onCreate Called", Toast.LENGTH_LONG).show() } override fun onStart() { super.onStart() // It will show a message on the screen // then onStart is invoked val toast = Toast.makeText(applicationContext, "onStart Called", Toast.LENGTH_LONG).show() } }
Java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Bundle containing previous frozen state setContentView(R.layout.activity_main); // The content view pointing to the id of layout // in the file activity_main.xml Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show(); } protected void onRestart() { // It will show a message on the screen // then onRestart is invoked Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called", Toast.LENGTH_LONG).show(); } }
Kotlin
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toast = Toast.makeText(applicationContext, "onCreate Called", Toast.LENGTH_LONG).show() } override fun onRestart() { super.onRestart() // It will show a message on the screen // then onRestart is invoked val toast = Toast.makeText(applicationContext, "onRestart Called", Toast.LENGTH_LONG).show() } }
Java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.example.share.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // Bundle containing previous frozen state super.onCreate(savedInstanceState); // The content view pointing to the id of layout // in the file activity_main.xml setContentView(R.layout.activity_main); Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show(); } protected void onResume() { // It will show a message on the screen // then onResume is invoked Toast toast = Toast.makeText(getApplicationContext(), "onResume Called", Toast.LENGTH_LONG).show(); } }
Kotlin
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toast = Toast.makeText(applicationContext, "onCreate Called", Toast.LENGTH_LONG).show() } override fun onResume() { super.onResume() // It will show a message on the screen // then onResume is invoked val toast = Toast.makeText(applicationContext, "onResume Called", Toast.LENGTH_LONG).show() } }
Java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // Bundle containing previous frozen state super.onCreate(savedInstanceState); // The content view pointing to the id of layout // in the file activity_main.xml setContentView(R.layout.activity_main); Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show(); } protected void onPause() { // It will show a message on the screen // then onPause is invoked Toast toast = Toast.makeText(getApplicationContext(), "onPause Called", Toast.LENGTH_LONG).show(); } }
Kotlin
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toast = Toast.makeText(applicationContext, "onCreate Called", Toast.LENGTH_LONG).show() } override fun onPause() { super.onPause() // It will show a message on the screen // then onPause is invoked val toast = Toast.makeText(applicationContext, "onPause Called", Toast.LENGTH_LONG).show() } }
Java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // Bundle containing previous frozen state super.onCreate(savedInstanceState); // The content view pointing to the id of layout // in the file activity_main.xml setContentView(R.layout.activity_main); Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show(); } protected void onStop() { // It will show a message on the screen // then onStop is invoked Toast toast = Toast.makeText(getApplicationContext(), "onStop Called", Toast.LENGTH_LONG).show(); } }
Kotlin
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toast = Toast.makeText(applicationContext, "onCreate Called", Toast.LENGTH_LONG).show() } override fun onStop() { super.onStop() // It will show a message on the screen // then onStop is invoked val toast = Toast.makeText(applicationContext, "onStop Called", Toast.LENGTH_LONG).show() } }
Java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // Bundle containing previous frozen state super.onCreate(savedInstanceState); // The content view pointing to the id of layout // in the file activity_main.xml setContentView(R.layout.activity_main); Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show(); } protected void onDestroy() { // It will show a message on the screen // then onDestroy is invoked Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called", Toast.LENGTH_LONG).show(); } }
Kotlin
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toast = Toast.makeText(applicationContext, "onCreate Called", Toast.LENGTH_LONG).show() } override fun onDestroy() { super.onDestroy() // It will show a message on the screen // then onDestroy is invoked val toast = Toast.makeText(applicationContext, "onDestroy Called", Toast.LENGTH_LONG).show() } }
Java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show(); } protected void onStart() { super.onStart(); Toast toast = Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_LONG).show(); } @Override protected void onRestart() { super.onRestart(); Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called", Toast.LENGTH_LONG).show(); } protected void onPause() { super.onPause(); Toast toast = Toast.makeText(getApplicationContext(), "onPause Called", Toast.LENGTH_LONG).show(); } protected void onResume() { super.onResume(); Toast toast = Toast.makeText(getApplicationContext(), "onResume Called", Toast.LENGTH_LONG).show(); } protected void onStop() { super.onStop(); Toast toast = Toast.makeText(getApplicationContext(), "onStop Called", Toast.LENGTH_LONG).show(); } protected void onDestroy() { super.onDestroy(); Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called", Toast.LENGTH_LONG).show(); } }
Kotlin
import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toast = Toast.makeText(applicationContext, "onCreate Called", Toast.LENGTH_LONG).show() } override fun onStart() { super.onStart() val toast = Toast.makeText(applicationContext, "onStart Called", Toast.LENGTH_LONG).show() } override fun onRestart() { super.onRestart() val toast = Toast.makeText(applicationContext, "onRestart Called", Toast.LENGTH_LONG).show() } override fun onPause() { super.onPause() val toast = Toast.makeText(applicationContext, "onPause Called", Toast.LENGTH_LONG).show() } override fun onResume() { super.onResume() val toast = Toast.makeText(applicationContext, "onResume Called", Toast.LENGTH_LONG).show() } override fun onStop() { super.onStop() val toast = Toast.makeText(applicationContext, "onStop Called", Toast.LENGTH_LONG).show() } override fun onDestroy() { super.onDestroy() val toast = Toast.makeText(applicationContext, "onDestroy Called", Toast.LENGTH_LONG).show() } }
Publicación traducida automáticamente
Artículo escrito por piyush25pv y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA