Skip to main content

Understanding Activities

This blog begins by showing you to create an activity. To create an activity , you create a Java class that extends the Activity base class :

public class MainActivity extends AppcompatActivity
{
          @Override
          protected void onCreate(Bundle savedInstanceState)
          {
                    super.onCreate(savedInstanceState);
                    setContentView(R.laout.activity_main);
          }
}

Your activity class loads its user interface ( UI ) component using the XML file defined in your res/layout folder . In this example , you would load the UI from the main.xml file :

          setContentView(R.layout.activity_main);

Every activity you have in your application must be declared in your AndroidManifest.xml file , like this :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android package="com.jfdimarzio.hellworld">
         
 <application
       android:allBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:supportRtl="true"
       android:theme="@style/AppTheme">
           <activity android:name=".MainActivity">
               <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
               </intent-filer>
            </activity>
 </application>

</maifest>


The Activity base class defines a series of events that govern the life cycle of an activity . Figure shows the lifecycle of an Activity.


The Activity class defines the following events:

  • onCreate( ) - Called when the activity is first created
  • onStart( ) - Called when the activity becomes visible to the user.
  • onResume( ) - Called when the activity starts interacting with the user
  • onPause( ) - Called when the current activity is being paused and the previous activity is being resumed
  • onStop( ) - Called when the activity is no longer visible to the user
  • onDestroy( ) - Called before the activity is destroyed by the system ( either manually or by the system to conserve memory )
  • onRestart( ) - Called when the activity has been stopped and is restarting again.
By default , the activity created for you contains the onCreate( ) event . Within this even handler is the code that helps to display the UI elements of your screen.

show the life cycle of an activity and the various is goes through - from when the activity is started until it ends.

The best way to understand the various stages of an activity is to create a new project , implement the various events , and then subject the activity to various user interactions.

Comments

Popular posts from this blog

Creating Android Virtual Devices (AVDs)

The next step is to create an Android Virtual Device (AVD) you can use for testing your Android application. An AVD is an emulator instance that enables you to model an actual device. Each AVD consists of a hardware profile ; a mapping to a system image ; and emulated storage,such as a secure digital (SD) card.One important thing to remember about emulators is that they are not perfect. There are some applications , such as games (which are GPU heavy ) or application that use sensors such as the GPS or accelerometer. Theses types of application cannot be simulated with the same speed or consistency within an emulator as they can when running on an actual device. However , the emulator is good for doing some generalized testing of your applications. You can create as many AVDs as you want to test your applications with different configurations. This testing is important to confirm the behavior of you application when it is run on different devices with varying capabilities. Use ...

Hiding the Activity Title

You can also hide the title of an activity if desired ( Such as when you just want to display a status update to the user). To do so , use the requestWindowFeature() method and pass it the window .FEATURE_NO_TITLE constant, like This : import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Window; public class MainActivity extends AppCompatActivity{            @Override            protected void onCreate ( Bundle savedInstanceState )            {                         super.onCreate (savedInstanceState );                         setContentView (R.layout.activit...

Applying Styles and Themes to an Activity

By default , an activity is themed to the default Android theme. However , there been a push in recent year to adopt a new theme known as Material . The Material theme has a much more modern and clean look to it. There are two versions of the Material theme available to Android developers : Material Light and Material Dark , Either of these themes can be applied from the AndroidManifest.xml To apply one of the Material themes to an activity , simply modify the <Application> element in the AndroidManifest.xml file by changing the default android:theme attribute. (Please be sure to change all instances of "com.android" to whatever package name your project is using.) <?xml version="1.0" encoding="utf-8"?> <manifest xmls:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        package="com.android.activity101">        <application ...