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

Android Studio

The first and most important piece of software you need to download is Android Studio. After you have downloaded and installed Android Studio , you can use the SDK Manager to download and install multiple versions of the Android SDK. Having multiple version of the SDK available enables you to write programs that target different devices. For example , you can write one version of an application that specifically targets Android Nougat , but because that flavor Android is on less than 1% of devices, with multiple versions of the SDK you can also write a version of your app that uses older features and targets Marshmallow or Lollipop users. You can use Android Device Manager to set up device emulators. You can Download Android Studio from https://developer.android.com/studio Android Studio is packaged in an executable. Run the install process to set up Android Studio. After you've download and run the setup executable, use the following steps to go through the installation proce...

What is Android

Android is a moblie operating System that is based on a modified version of Linux. It was originally developed by a startup of the same name , Android , Inc. In 2005 , as part of its strategy to enter the moblie space , Google purchased Android , Inc. and took over its development work (as well as its development team). Google wanted the Android OS to be open and free , so most of the Android code was released under the open source Apache License. That means anyone who wants to use Android can do so by downloading the full Android source code. Moreover, vendors ( typically hardware manufacturers ) can add their own proprietary extensions to Android and customize Android to differentiate their products from others . This development model makes Android very attractive to vendors, especially those companies affected by the phenomenon of Apple's iPhone,which was a hugely successful product that revolutionized the smartphone industry. When the iPhone was launched , many smartphone m...

Exploring The IDE

In this section you explore the Android Studio Integrated Development Environment, Which is also known as the IDE. Basically, the IDE is the interface between you and Android Studio. The more you know about the tools, windows , and options that are available to you in Android Studio , the faster you will be able to produce code and the more confident you will be at creating applications. 1. If you haven't already, open Android Studio. If you worked through last Blog , you created a very quick Hello World project. You are going to create another quick project for this blog ; this time , however , you explore the different options available as you start up and work with your project. 2. Now that you have opened Android Studio , you see should a screen that looks like show NOTE :- if you not see the screen in figure and instead see the last Hello world Project from last blog , simply locate the File option on the menu bar , Click File -> Close Project to return to the scr...