Android
    About Lesson

    In Android, an Activity is a single, focused thing that the user can do. It is a crucial component of any Android app, and they are entry points through which users interact with your app. Let’s delve into the details of Android Activities.

    What is an Activity?

    An Activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email and another activity for reading emails.

    Activity Lifecycle

    Each activity in an Android app goes through a lifecycle that manages its transition into and out of the foreground of the user interface. The lifecycle is defined by a series of callback methods:

    • onCreate(): This is the first callback and is called when the activity is first created.
    • onStart(): This callback is called when the activity becomes visible to the user.
    • onResume(): This is called when the user starts interacting with the application.
    • onPause(): The paused activity does not receive user input cannot execute any code and is called when the current activity is being paused and the previous activity is being resumed.
    • onStop(): This callback is called when the activity is no longer visible.
    • onDestroy(): This callback is called before the activity is destroyed by the system.
    • onRestart(): This callback is called when the activity restarts after stopping it.

    Managing Activity State

    As your activity enters and exits the foreground of the user interface, it transitions between various states in its lifecycle. You can use these lifecycle callbacks to ensure your activities gracefully handle transitions between states and properly maintain their state when they are not in the foreground.

    Navigating Between Activities

    Activities in an app are arranged in a stack-like structure, with the current activity at the top of the stack. One activity can start another, including one defined in a different app, by calling startActivity(). When a new activity starts, it is pushed onto the stack and takes user focus.

     

    package in.infovistar.helloworld;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    
    public class MainActivity extends Activity {
       String msg = "Infovistar Android : ";
       
       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Log.d(msg, "The onCreate() event");
       }
    
       /** Called when the activity is about to become visible. */
       @Override
       protected void onStart() {
          super.onStart();
          Log.d(msg, "The onStart() event");
       }
    
       /** Called when the activity has become visible. */
       @Override
       protected void onResume() {
          super.onResume();
          Log.d(msg, "The onResume() event");
       }
    
       /** Called when another activity is taking focus. */
       @Override
       protected void onPause() {
          super.onPause();
          Log.d(msg, "The onPause() event");
       }
    
       /** Called when the activity is no longer visible. */
       @Override
       protected void onStop() {
          super.onStop();
          Log.d(msg, "The onStop() event");
       }
    
       /** Called just before the activity is destroyed. */
       @Override
       public void onDestroy() {
          super.onDestroy();
          Log.d(msg, "The onDestroy() event");
       }
    }