An activity is a single screen in android. It is like a window or frame in Java. With the help of activity, you can place all your UI components or widgets on a single screen. The activity can be in different states depending on how it is interacting with the user. These states are described as,
Running:
Activity is visible and interacts with the user.
Paused:
Activity is still visible but partially obscured, instance is running but might be killed by the system.
Stopped:
Activity is not visible, instance is running but might be killed by the system.
Killed:
Activity has been terminated by the system of by a call to its finish() method.
Android Activity Lifecycle ;
Is controlled by 7 methods of the android.app.Activity class. The 7 lifecycle method of Activity describes how the activity will behave at different states.
onCreate()
Called then the activity is created. Used to initialize the activity, for example create the user interface.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(“lifecycle”,”onCreate invoked”);
}
onStart()
onStart() is called when the activity is becoming visible. This is an ideal place to write code that affects the UI of the application, such as an event that deals with user interaction. This callback is normally followed by onResume but could be followed by onStop if the activity becomes hidden.
@Override
protected void onStart() {
super.onStart();
Log.d(“lifecycle”,”onStart invoked”);
}
onResume()
Called if the activity get visible again and the user starts interacting with the activity again. Used to initialize fields, register listeners, bind to services, etc.
@Override
protected void onResume() {
super.onResume();
Log.d(“lifecycle”,”onResume invoked”);
}
onPause()
Called once another activity gets into the foreground. Always called before the activity is not visible anymore. Used to release resources or save application data. For example you unregister listeners, intent receivers, unbind from services or remove system service listeners.
@Override
protected void onPause() {
super.onPause();
Log.d(“lifecycle”,”onPause invoked”);
}
onStop()
Called once the activity is no longer visible. Time or CPU intensive shut-down operations, such as writing information to a database should be down in the onStop() method. This method is guaranteed to be called as of API 11.
@Override
protected void onStop() {
super.onStop();
Log.d(“lifecycle”,”onStop invoked”);
}
onDestroy()
onDestroy() is called by the system before the activity is destroyed, either because the activity is finishing or because the system is reclaiming the memory the activity is using.
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(“lifecycle”,”onDestroy invoked”);
}
onRestart()
onRestart() is called when the activity is being restarted, as when the activity is returning to the foreground. It is always followed by onStart.
@Override
protected void onRestart() {
super.onRestart();
Log.d(“lifecycle”,”onRestart invoked”);
}
References:
https://developer.android.com/
Find code on: