0% found this document useful (0 votes)
72 views

Mobile Application Develpment: Android Activities

An Android activity represents a single screen in an app with a user interface. Activities are launched to transition between different app screens. Activities have lifecycles with states like resumed, paused, and stopped that are called when moving between the foreground and background. Common activity methods include onCreate, onStart, onResume, onPause, and onDestroy. Activities are organized in a stack, with the current activity on top.

Uploaded by

ena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Mobile Application Develpment: Android Activities

An Android activity represents a single screen in an app with a user interface. Activities are launched to transition between different app screens. Activities have lifecycles with states like resumed, paused, and stopped that are called when moving between the foreground and background. Common activity methods include onCreate, onStart, onResume, onPause, and onDestroy. Activities are organized in a stack, with the current activity on top.

Uploaded by

ena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Mobile Application

Develpment
Android Activities
Activity Definition
 An activity represents a single screen with
a user interface (Java file + xml layout file).

 An app usually consists of multiple activities


that are loosely bound to each other.

 One Activity is flagged as “main” and it is


started at the application launch time.

 An Activity can launch other activities to create


an app UI workflow.

 The Activity has an Intent attribute that


determines how android treats it.
Activity
AppCompatActivity
States of an activity
 Starting State
 initial setup.
 Resumed/Running State
 visible, user interacting
 are considered active or running
if they are in the foreground.
 Paused State
 visible, user not interacting,
 can be terminated*
 if the device goes to sleep or if it
is covered with another
Activity partially or completely.
 Stopped State
 are stopped or in the background with
the lowest priority.
 not visible,
 can be terminated
Activity lifecycle

6
Some Activity Methods
 onCreate():
 is called when an Activity is getting created for the first time.
 onStart():
 used onStart to reset Activity data, reinitialize variables etc.
 onResume():
 gets called when an Activity comes into the foreground, and it becomes
visible to the user. At this point, the user can start interacting with the Activity.
 onPause():
 is called when another android activity comes on top of an Activity.
 onStop():
 is called when an Activity is no longer visible to the user, it is similar to
onPause but here you will not see your android activity entirely.
 onRestart():
 It is similar to onCreate, but onRestart gets called only after onStop.
 OnDestroy:
 This is the method which will be called when your Activity is getting killed.
This is the final call the Activity will receive in its Lifecycle.
Activity stack
 When a new activity is started, it is placed on the top of the stack
and becomes the running activity.
 The previous activity always remains below it in the stack.
 The previous activity will not come to the foreground again until the
new activity exits. (“Back stack”)
 Navigation forward/back triggered by user actions
Three different life cycles of an
activity
 The entire lifetime
 Starts by first call to onCreate(Bundle) and

ends by a single final call to onDestroy().


 The visible lifetime
 Starts by a call to onStart() and ends by
a corresponding call to onStop(). During this
time the user can see the activity on-screen,
though it may not be in the foreground and
interacting with the user.
 The foreground lifetime
 Starts by a call to onResume() and ends
a corresponding call to onPause(). During
this time the activity is in front of all other
activities and interacting with the user. An activity
can frequently go between the resumed and paused states.
Top-down design
Let's start from a design of an app that we want to create and
then learn the necessary skills to build that app.
● "Bigger Number" game
– user is shown two numbers
– must choose which one is bigger by
clicking on the appropriate button
– game pops up brief "correct" / "incorrect"
message after each guess
– get points for each correct answer
(lose points for incorrect answers)
Creating a new project
Designing a user interface
open XML file for your layout (e.g. activity_main.xml)
● drag widgets from left Palette to the preview image
● set their properties in lower-right Properties panel
Events

● event: An external stimulus your program can respond to.


● Common kinds of events include:
– Mouse motion / tapping, Keys pressed,
– Timers expiring, Network data available

● event-driven programming: Overall


execution of your program is largely dictated by user events.
– Commonly used in graphical programs.

● To respond to events in a program, you must:


– Write methods to handle each kind of event ("listener" methods).
– Attach those methods to particular GUI widgets.
Setting an event listener
 select the widget in the Design view
 •scroll down its Properties until you find onClick
 type the name of a method you'll write to handle the click
 •switch to the Text view and find the XML for that button
 •click the "Light Bulb" and choose to "Create" the method
Event listener Java code
Generated Java code
Displaying Toasts
Toast.makeText(this, "message", duration).show();
– where duration is Toast.LENGTH_SHORT or LENGTH_LONG
● A "Toast" is a pop-up message that appears for a short time.
● Useful for displaying short updates in response to events.
● Should not be relied upon extensively for important info.
References
 Activity class
 https://developer.android.com/reference/android/app/Activity.html
#Activity

 Getting Started
 https://developer.android.com/training/index.html

You might also like