3. 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
5. Activity Lifecycle States
● Active or running
the activity is in the foreground of the screen
● Paused
activity has lost focus but is still visible
activity is alive but can be killed if low in memory
● Stopped
activity is completely obscured by another activity
● Destroyed
activity is dropped from memory, killed/finished. to
display it again, it must be restarted
6. 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
7. 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.
10. MainActivity breakdown
● @Override onCreate(Bundle savedInstanceState)
the lifecycle callback
Bundle 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
11. R.java
R.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
13. View
represents 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
16. 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_height
The size of the view’s rectangular area relative to its
parent.
Example layout types:
match_parent - size matches the view’s parent
wrap_content - size is contained to the size of the
view’s content.
19. ViewGroup
a special view that can contain other views which are
then called child views.
Examples:
LinearLayout, RelativeLayout, TableLayout
20. LinearLayout
A 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
26. RelativeLayout
A Layout where the positions of the children can be
described in relation to each other or to the parent.
Example:
android:layout_above
android:layout_below
android:layout_toRightOf
android:layout_toLeftOf
30. 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
33. Input Events
Button 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
34. Input Events
button.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
36. Input Events
button.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
37. 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 Context
Activity 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
38. 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?
43. Intent
An 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.
46. MainActivity Code
Intent intent = new Intent() //instantiate intent object
//specify target activity
intent.setClass(this, OtherActivity.class)
//add extra values as a key-value pair
intent.putExtra(“KEY”, “VALUE”);
//callable within any Activity class
startActivity(intent);
47. Other_Activity Code
//Calling getIntent() will retrieve the intent used to launch
this activity
Bundle extras = getIntent().getExtras();
//getString() is used because we know that the key’s
value is a String object
String datas = extras.getString(“KEY”);
48. 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?