- 1 Activity in Android – Tutorial | AndroidStudio
- 1.1 Basics of Activities
- 1.2 Activities and Layout – What are they?
- 1.3 Activity Lifecycle
- 1.4 Where to from here?
- 1.5 Related Posts
Activity in Android – Tutorial | AndroidStudio
The activity class is a very important part of a Android Application. The paradigm shift in the world of smartphone applications begins here!!How exciting is it to know, from your whatsapp to facebook application, nothing can ever happen without the Activities.
Think of activities as the starting block, your Android OS looks for. The application begins from a activity, goes through a lot of stages and ends once the application closes. We will see, how an activity is created, ways to implement it and create a simple application with it.
Basics of Activities
Activities are the starting point for any application, you open a application, the OS shoots one and the appication opens it. Activities are responsible for holding a UI along with it as well. This provides us a clearer idea of what constitutes as an activity.
In the concept of Android Programming, there are some very important blocks that need to be known, understanding Activites and Layout is one of them.
The three important things to know when creating an application is, defining your Layout, creating your activity and understanding a Manifest. We will see how all of these are tied together and bundles to create a application.
Activities and Layout – What are they?
Coming on to the UI aspect that we aim to create, we will have to know what a Layout is. Android is not entirely Java alone, it also contains some XML aspect to it which constitutes the UI for us (Even though you can create a application without a XML also!). Below, we will see how the Layout and Activities are Created and Connected.
Ever wondered how texts are created in the android application. Know what is responsible for creating them? A brief definition of a Layout will be, anything that holds a UI to it will come under layout and can be created using a XML.
You will have to know about Views and ViewGroups and know the difference between them. Below is a very simple Layout you can include in your application.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Activity"/> </RelativeLayout>
This creates the Layout file under the layout>activity_main.xml like below.
This Layout now links with our activity to create our first application.
Great job so far creating our layout. How do you create activities and what is its use? Brief definition for that will be, anything that provides a action on the static layout we created above, it comes under activity. Activities are the component that will be holding a UI with it. From the creation till the destruction of the Android Application, it will be responsible for holding this UI.
You can create a simple Activity by going to the app>java>your_package(main) like below.
I have named my activity as MainActivity and the package is com.monks.android.justmultiply. The empty class looks like below.
What do you see above. If you have guessed it right, you get a +1. Yes, we have linked our Layout to the Activity using the setContentView() method. Inititally, this might seem overwhelming with all the other callbacks and confusing syntax, but try to understand what they do. It will make your life easier.
The third piece in our puzzle is to know about a Manifest. What does it do? Brief definition – You want to create an activity that the user is going to see? Include it in the Manifest (For now, lets stick to manifest there are other things as well which you have to include). Manifest is the first file that a OS searches for. This file solely holds all the important details that a OS has to know about in order to launch the Application. If you have included a Activity in it, it means the user is required to see it.
With this in mind, check the below file. Manifest is found in app>manifests
This file is auto-generated, and contains whatever has to be seen by the user. There are various tags which hold lot of different things, but for now lets play along with this auto generated Manifest. This will be enough to create our application.
The important part to know when it comes to the Activities are about how its Lifecycle (Start to End) looks like.
This image has got lot of things in it and we will try to understand what each and every step means. The main callbacks included in an activity lifecycle is its onCreate() and onStop() callbacks. Think about callbacks as stages in the Activity.
The above diagram contains primarily 6 callbacks out of which we will have to override only the onCreate() callback to create our simple application like we saw above.
See below for a detailed definition to each lifecycle stage.
You must implement this callback, which fires when the system creates your activity. Your implementation should initialize the essential components of your activity: For example, your app should create views and bind data to lists here. Most importantly, this is where you must callÂ
setContentView()Â to define the layout for the activity’s user interface.
onCreate()Â exits, the activity enters the Started state, and the activity becomes visible to the user. This callback contains what amounts to the activityâs final preparations for coming to the foreground and becoming interactive.
The system invokes this callback just before the activity starts interacting with the user. At this point, the activity is at the top of the activity stack, and captures all user input. Most of an appâs core functionality is implemented in theÂ
The system callsÂ
onPause()Â when the activity loses focus and enters a Paused state. This state occurs when, for example, the user taps the Back or Recents button. When the system callsÂ
onPause()Â for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity, and the activity will soon enter the Stopped or Resumed state.
An activity in the Paused state may continue to update the UI if the user is expecting the UI to update. Examples of such an activity include one showing a navigation map screen or a media player playing. Even if such activities lose focus, the user expects their UI to continue updating.
You shouldÂ notÂ useÂ
onPause()Â to save application or user data, make network calls, or execute database transactions.
The system callsÂ
onStop()Â when the activity is no longer visible to the user. This may happen because the activity is being destroyed, a new activity is starting, or an existing activity is entering a Resumed state and is covering the stopped activity. In all of these cases, the stopped activity is no longer visible at all.
The system invokes this callback when an activity in the Stopped state is about to restart.Â
onRestart()Â restores the state of the activity from the time that it was stopped.
This callback is always followed byÂ
The system invokes this callback before an activity is destroyed.
This callback is the final one that the activity receives.Â
onDestroy()Â is usually implemented to ensure that all of an activityâs resources are released when the activity, or the process containing it, is destroyed.
Where to from here?
The first two sections creates a complete overview for you to understand how the application works with and without the help of an entity called as the Acitivity. You can check out the sections here, to get started on creating your application from scratch.
Drop in any comments you have below.
“Learn and Be Curious”