Android development - Activities, Views & Intents

Post on 26-Jun-2015

304 views 3 download

Tags:

description

Android development - Activities, Views & Intents

Transcript of Android development - Activities, Views & Intents

Android DevelopmentActivities, Views & Intents

by Lope Emano

Activity

Activity● provides the user a screen/interface in order to

interact with the application● android applications are generally made up of

activities● activities have their own “lifecycles” in order to

manage multiple activities and lessen their impact to a device’s memory and resources

● activities have 4 states throughout its lifecycle● an activity’s lifecycle has a series of callbacks that we

can leverage on when building apps

The Activity Lifecycle:States and Callbacks

Activity Lifecycle States● Active or running

the activity is in the foreground of the screen● Paused

activity has lost focus but is still visibleactivity is alive but can be killed if low in memory

● Stoppedactivity is completely obscured by another activity

● Destroyedactivity is dropped from memory, killed/finished. to

display it again, it must be restarted

Activity Lifecycle Callbacks● onCreate()

This is the first callback and called when the activity is first created.● onStart()

This callback is called when the activity becomes visible to the user.● onResume()

This is called when the user starts interacting with the application.● onPause()

Where you deal with the user leaving your activity. Any changes made by the user should at this point be committed

Activity Lifecycle Callbacks● onStop()

Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one

● onDestroy()The final call you receive before your activity is

destroyed.● onRestart()

Called after your activity has been stopped, prior to it being started again.

Sample activity overriding the onCreate() callback

Let’s break that down

MainActivity breakdown● @Override onCreate(Bundle savedInstanceState)

the lifecycle callbackBundle object contains extra data when the activity

gets started (more on this later)

● super.onCreate()invokes the onCreate callback of the parent class

to maintain consistent behaviour

● setContentView(R.layout.activity_main)sets the layout for the activity

R.javaR.java or “R” connects java code and resource files contained in the src/main/res folder

This file is auto generated when compiling the code. You should not be editing this.

Example:src/res/layout/activity_main.xml --- R.layout.activity_main

src/res/drawable-hdpi/ic_launcher.png --- R.drawable.ic_launcher

Android Views

Viewrepresents the basic building block for user interface components

occupies a rectangular area on the screen and is responsible for drawing and event handling

Examples:Button, TextView, EditText, ImageView, RadioButton, Checkbox

Declaring a View

Break it down

Declaring a View● <TextView></TextView>

views are defined in xml

● android:id=”@+id/txt_hello_world”One of the many tags a view can have. this one

specifies the id of the given view. This will be accessible later in our java code via R.id.txt_hello_world

● android:layout_width & android:layout_heightThe size of the view’s rectangular area relative to its

parent.Example layout types:

match_parent - size matches the view’s parentwrap_content - size is contained to the size of the

view’s content.

fill_parent vs wrap_content

ViewGroups

ViewGroupa special view that can contain other views which are then called child views.

Examples:LinearLayout, RelativeLayout, TableLayout

LinearLayoutA Layout that arranges its children in a single column

or a single row

The direction of the row can be set by setting android:orientation=”horizontal|vertical”

The default direction is horizontal

LinearLayout w/ vertical orientation

LinearLayout w/ vertical orientation

You can nest them

RelativeLayout

RelativeLayoutA Layout where the positions of the children can be

described in relation to each other or to the parent.

Example:

android:layout_aboveandroid:layout_belowandroid:layout_toRightOfandroid:layout_toLeftOf

Handling Input Events

Input Events● onClick()

From View.OnClickListener. ● onLongClick()

From View.OnLongClickListener. ● onFocusChange()

From View.OnFocusChangeListener. Called when the user navigates onto or away from the item

● onKey()From View.OnKeyListener. Called when the user is focused on the item and presses or releases a hardware key on the device.

● onTouch()From View.OnTouchListener. Called when the user performs an action qualified as a touch event

Input Events● onClick() example

Break it down

Input EventsButton button = (Button) findViewById(R.id.btnLogin);● a Button is a type of View or Button inherits View● findViewById() is accessible within the Activity class.● (Button) is type casting in java. We need to do this

because findViewById() returns a View object.● R.id.btnLocation

R is a reference for R.java, the bridge between java and xml code. N

Note the btnLocation naming convention

Input Eventsbutton.setOnClickListener(new View.OnClickListener{});● setOnClickListener is inherited from the View class● View.OnClickListener is a java interface. When

instantiating an interface directly you must supply its required methods.

Toast.makeText(getApplicationContext(), "You clicked me!", Toast.LENGTH_SHORT);● Toast provides simple feedback about an operation in

a small popup.● Toast.LENGTH_SHORT, Toast.LENGTH_LONG are

integers that return the toast’s display duration

Toast being used in Android’s Alarm App

Input Eventsbutton.setOnClickListener(new View.OnClickListener{});● setOnClickListener is inherited from the View class● View.OnClickListener is a java interface. When

instantiating an interface directly you must supply its required methods.

Toast.makeText(getApplicationContext(), "You clicked me!", Toast.LENGTH_SHORT).show();● Toast provides simple feedback about an operation in

a small popup.● Toast.LENGTH_SHORT, Toast.LENGTH_LONG are

integers that return the toast’s display duration● show() to display

getApplicationContext()● the context of current state of the application/object

and lets newly created object understand what’s going on

● you’ll be using this a lot as an android developer, many objects will require it

● 2 kinds of ContextActivity Context - Accessible via getContext or this

within an Activity class

Application Context - The context of the entire application. Use this if you want something to be hooked to the app’s lifecycle instead of only a specific activity

Suppose we have an activity that shows a list and when we tap on

an item in that list we’d like to view it in another activity. How

would we do that?

In other words

How do we launch another activity with parameters?

Intent

IntentAn abstract description of an operation to be

performed

Its most significant use is in the launching of activities, where it can be thought of as the glue between activities

2 Primary pieces of information:

● action - The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

● data - The data to operate on, such as an ID, url, or even image taken from a camera activity.

Break it down

MainActivity CodeIntent intent = new Intent() //instantiate intent object//specify target activityintent.setClass(this, OtherActivity.class)//add extra values as a key-value pairintent.putExtra(“KEY”, “VALUE”);//callable within any Activity classstartActivity(intent);

Other_Activity Code//Calling getIntent() will retrieve the intent used to launch this activityBundle extras = getIntent().getExtras();//getString() is used because we know that the key’s value is a String objectString datas = extras.getString(“KEY”);

What we’ve learned● A lot. Like many other frameworks, reading the docs

and hunting down resources in the internet is important as an android developer.

● Activities have their own lifecycles and states.● Xml syntax for views can be taxing, but you’ll get used

to it!● There can be many ways to create a layout using

ViewGroups, we just need to find the most efficient/clean.

● Intents can be tedious since you have to know certain things in order to do them properly. i.e. what if you want to bundle an object instead of a POJO?