Activity Class | Android Tutorials #3

After creating an android project, the first thing you will notice is the activity class. An activity is the primary class for user interaction, it is a single-focused task that a user can do.

Activity Class

Each activity should be modular and support one-focused thing a user can do in an app. For example, one that handles only the login of your application. It handles and takes care of the UI you will need for that specific window, using setContentView(view)

Activity Life Cycle

In simple words, an activity can be divided in three states:

  • Resumed/Running State: visible, user interacting
  • Paused: Visible, user not interacting, can be terminated
  • Stopped: Stopped, no longer visible, android terminates it.


As we can see in the image and code-wise, it contains 7 methods used in the life cycle:

  • onCreate()
  • Called when the Activity is created, it set ups the initial state. It does the following 4 functions:
    – Call super.onCreate();
    – set the activities contentView, which tells android what the activity user interface should be
    – retain reference to UI views as necessary
    – Configure views as necessary

  • onRestart()
  • Called if the activity has been stopped and is about to be started again
    Typical Actions:
    – Special processing needed only after having been stopped

  • onStart()
  • Called when the activity is about to get visible
    Typical Actions:
    – Start when visible-only behaviors
    – Loading persistent application state

  • onResume()
  • Called when activity is visible and about to start interacting with user
    Typical actions:
    – Start foreground-only behaviours, like animations or background soundtrack

  • onPause()
  • Called when an activity is about to change to another activity
    Typical actions:
    – Shutdown foreground-only behaviours, like animations
    – Save persistant state

  • onStop()
  • Called when activity no longer visible, may be restarted later
    Typical actions:
    – Cache state
    Note: May not be called if Android kills your application

  • onDestroy()
  • Called when activity is about to be destroyed
    Typical Actions:
    – Release activity resources, like private threads
    Note: May not be called if Android kills your application

Starting Activities

In order to start an activity in a simple way, you either set it as the main activity in the AndroidManifest.xml or in any activity, create an intent object specifying the activity to start, pass newly created intent to methods such as:

  • startActivity()
  • startActivityForResult(), which invokes a callback method when the called activity finishes to return a result


Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);

If you notice that we flip the phone from portrait to landscape or vice-versa, any data we would have it will be lost. On configuration changes, Android usually kills the current activity and then restarts it, therefore an activity restarting should be fast, if necessary, we can retain an object containing important state information, savedInstanceState, or we could manually handle any configuration change. We could also force the screen orientation to one or another, in order to avoid dealing with this part. This is done in the wanted activity inside AndroidManifest.xml.

Retaining objects…

Hard to recompute data cna be cached in to speed up the handling of configuration changes. We will need to override onretainNonConfigurationIntsance() to build and return the configuration object. This will be called somewhere between onStop() and onDestroy(). To recover retained object, getLastNonConfiugrationInstance() should be called during onCreate() to recover retained object.

Note: these methods have been deprecated in favor of methods in the fragment class.

The other way would be manual re-configuration, which can prevent system from restarting an activity. We can declare configuration changes used in your activity and being handled in the AndroidManifest.xml
When configuration changes:

    Activities onConfigurationChanged() method is called, passing a configuration object specifying the new device configuration

Concluding this tutorial, learning the activity class life cycle and it’s components and help you understand and solve any problems you might have in the future. If you have any further questions or comments, feel free to write a comment or email me, I will answer as soon as possible.