Friday 6 January 2017

Android Activity Step By Step

As we have discussed earlier that there are mainly 4 components of Android so now we will come to know in detail about them one by one .

In todays blog we are going to discuss Android's main component Activity.

Definition 

Activity is an user interface with help of which a user can interact with any mobile app. It shows all the essential controls like navigation menu,settings icon etc. with help of which user can click on screen and can use the app without knowing about the background tasks that are going on. Example a simple example we can take is like as when we are working on computer we use an operating system whether it is an IOS or Windows or Ubuntu we only click on menu that is shown to us we do not know what is going on in the background. 

In the same way Activity  provides a user an interface with which user interacts with the mobile application.

How to create an Activity Step By Step - 

1. Create a project using Android Studio as shown in below diagram -


2. Give your application a name as you wanted to and in company domain edit text area you can give any name and at last the package name that must be unique for your app it is used to identify your app while you upload the app to play store so keep it unique.


3. Select the Minimum SDK version it is used to configure your app for the devices that  are using the SDK version above you selects here . For the below SDK version your app will not be available or will not run on the devices. SO select the minimum SDK version to reach max number of users.


4. Select and Activity layout as per requirement as here I am selecting an empty activity .


5.  Name your activity as per your requirement here i am not changing it leaving as Main activity.


6. When we click on Finish button we have the screen like below by default Android Studio has created an XML file , a JAVA  class file and an activity method onCreate.

Here XML files are used to create design of activity classes and then we attach them to JAVA class files or we can say we set the content to our activity using a method setContentView and passes the id to this method as its argument. To access the files or any views contained by XML file we use R.id keyword to access the resources.

Here R stands for resources it contains the id of all resources that we have created in XML file it stores the id in Hexadecimal format. It is strongly recommended that you should never alter the R file. We define the id for views in XML file when we defines them.

We will discuss the views and view groups in upcoming blogs now we are going for only basics.


7. Now before discussing further about the activity you must know about what is Android Activity Life Cycle.Have a look on below image-


Hope you are getting some idea from the image itself now we are going to discuss all methods one by one-

1. onCreate

This is first callback method which is called when activity is created generally here we do all the initialisation and defines our views that we are going to use in Activity.Here we set the content view to activity or attach the XML/Design part .

2. onStart 

This method is called when activity becomes visible to the user.

3. onResume

When this method is called then user can interact with our application .

4. onPause

This method is called when our activity is in background may be because of we pressed the home button key or some other activity comes in foreground.

5. onStop

This method is called after onPause here activity is no more visible to user and user can not interact with the application.

6. onRestart

This method is called when our activity comes to foreground after it got stop earlier.

7. onDestroy

This method is called when we press back button to exit from application or we remove the application from recent activities in our mobile after this method call application get created again .

Note- All of these methods are called in background automatically. we call them if we need to do something then we can override them as shown in image below-


Here I have used Log class to print the output as error in our logcat window.

8. Now run the application it will show the android devices that are attached to system.We can create Android Virtual Device from the menu icon itself.We will discuss that in another blog.You can also download third party virtual devices like Genymotion.


9. When we run our application it will look like it is shown in below image. Here you can see in red text that three methods are called onCreate,onStart,onResume after that activity is visible to us.


10. When I press the home key of Virtual device the two methods are called onPause,onStop and our activity/Application goes in background.


11. When I get it back from recent activities then three methods are called onRestart,onStart and onResume after that our activity becomes visible to us again.


12. When I press the back key then only three methods are called onPause,onStop and onDestroy after that our application is get destroyed and no more visible to the user.


This is all about the Android application Activity life cycle and after defining activity how we can use this on our application. Whenever we create an Activity we need to define the activity in our application's Manifest file as shown below-

<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...</manifest >
To make our activity as first launcher activity we need to define Intent Filter as show below-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tutorialspoint7.myapplication">

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="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-filter>
        </activity>
    </application>
 </manifest>
 

No comments:

Post a Comment