Chapter 5 Android User Interface
Chapter 5 Android User Interface
CHAPTER 5
✓ Activities
✓ Creating Activities
✓ Installing application
✓ Responding to error
Activity
• In android, Activity represents a single screen with a user interface (UI) of an
application and it will act an entry point for users to interact with an app.
• Generally, the android apps will contain multiple screens and each screen of our
application will be an extension of Activity class. By using activities, we can place all our
android application UI components in a single screen.
• From the multiple activities in android app, one activity can be marked as a main
activity and that is the first screen to appear when we launch the application.
• In android app each activity can start another activity to perform different actions based
on our requirements.
• Generally, in android there is a minimal dependency between the activities in an app. To
use activities in application we need to register those activities information in our app’s
manifest file (AndroidMainfest.xml) and need to manage activity life cycle properly.
• To use activities in our application we need to define an activity with required attributes
in manifest file (AndroidMainfest.xml) like as shown below
DDWD2713 – Mobile Programming 2
• By using activity callback methods, we can define how our activity can behave when the
user enters or leaves our application.
1. onCreate()
• First callback method, and it fires when the system creates an activity for the first time.
• If we have an application start-up logic that needs to perform only once during the life
cycle of an activity, then we can write that logic in onCreate() method.
• Following is the example of defining a onCreate() method in android activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
DDWD2713 – Mobile Programming 3
2. onStart()
• The onStart() callback method will invoke after onCreate() method completed.
• Following is the example of defining a onStart() method in android activity.
@Override
protected void onStart()
{
super.onStart()
}
3. onResume()
• After completion of onStart() method execution, the activity enters into Resumed
state and system invoke the onResume() method.
• In case if any interruption events happen in Resumed state, the activity will enter
into Paused state and the system will invoke onPause() method.
• After an activity returned from Paused state to Resumed state, the system again will
call onResume() method due to this we need to implement onResume() method to
initialize the components that we release during onPause() method
• Following is the example of defining a onResume() method in android activity.
@Override
public void onResume() {
super.onResume();
if (mCamera == null) {
initializeCamera();
}}
DDWD2713 – Mobile Programming 4
4. onPause()
• Whenever the user leaves an activity or the current activity is being Paused then the
system invokes onPause() method.
• Following is the example of defining a onPause() method in android activity.
@Override
public void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
5. onStop()
• The system will invoke onStop() callback method when an activity no longer visible to
the user, the activity will enter into Stopped state.
• The onStop() method is useful to release all the app resources which are no longer
needed to the user.
• Following is the example of defining a onStop() method in android activity.
@Override
protected void onStop()
{
super.onStop();
}
• The next callback method which raised by the system is either onRestart(), in case if
the activity coming back to interact with the user or onDestroy(), in case if the
activity finished running.
DDWD2713 – Mobile Programming 5
6. OnRestart()
• The system will invoke onRestart() method when an activity restarting itself after
stopping it. The onRestart() method will restore the state of activity from the time
that is being stopped.
7. onDestroy()
• The system will invoke onDestroy() method before an activity is destroyed and this is
the final callback method received by the android activity.
• The system will invoke this onDestory() callback method either the activity is finishing
or system destroying the activity to save space.
@Override
public void onDestroy()
{
super.onDestroy();
}
• The onDestroy() method will release all the resources which are not released by
previous callback onStop() method.
DDWD2713 – Mobile Programming 6
Following is the pictorial representation of the Android Activity Life cycle which shows how
Activity will behave in different stages using callback methods.
DDWD2713 – Mobile Programming 7
package com.tutlane.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Activity Lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("Activity Lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("Activity Lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("Activity Lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("Activity Lifecycle","onStop invoked");
}
DDWD2713 – Mobile Programming 8
@Override
protected void onRestart() {
super.onRestart();
Log.d("Activity Lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("Activity Lifecycle","onDestroy invoked");
}
}
3. After a while, the activity will enter into Stopped state and system will
invoke onStop() method like as shown below.
4. Now again launch our app from the Home screen list of apps like as shown below.
DDWD2713 – Mobile Programming 11
6. Now click on Back button in the android emulator, the system will invoke
the onPause method and after a while onStop, onDestroy methods will be invoked
like as shown below.
Here we need to remember that onCreate and onDestroy methods will invoke only
once throughout the activity life cycle.
DDWD2713 – Mobile Programming 12
This is how android activity life cycle process will invoke different methods while
transition from one stage to another stage.
Running App
• A running app is one that user is currently using or interacting with
• Visible, in foreground
DDWD2713 – Mobile Programming 13
Paused App
• An app is paused if it is visible but no longer in foreground
o E.g. blocked by a pop-up dialog box
• App’s onPause( ) method is called during transition from running to paused state
• Typical actions taken in onPause( ) method
Paused
Running
DDWD2713 – Mobile Programming 14
Paused
DDWD2713 – Mobile Programming 15
Stopped App
• An app is stopped if it no longer visible and no longer in foreground
o E.g. user starts using another app
• App’s onStop( ) method is called during transition from paused to stopped state
• An activity is stopped when:
o User receives phone call
o User starts a new application
o Activity 1 launches new Activity 2
o Activity instance and variables of stopped app are retained but no code is
being executed by the activity
• If activity is stopped, in onStop( ) method, well behaved apps should
o save progress to enable seamless restart later
o Release all resources, save info (persistence)
DDWD2713 – Mobile Programming 16
• A stopped app can go back into running state if becomes visible and in foreground
• App’s onStart( ) and onResume( ) methods called to transition from stopped to
running state
Running
DDWD2713 – Mobile Programming 17
Rotating Device
• Rotating device (e.g. portrait to landscape) kills current activity and creates new
activity in landscape mode
• Rotation changes device configuration
• Device configuration: screen orientation/density/size, keyboard type, dock mode,
language, etc.
• Apps can specify different resources (e.g. XML layout files, images) to use for
different device configurations
• E.g. use different app layouts for portrait vs landscape screen orientation
DDWD2713 – Mobile Programming 19
• Go to app > src > main > res > right-click > New > Android Resource Directory
• Select Resource type as layout then go to Orientation and then click on the >> icon.
DDWD2713 – Mobile Programming 20
• Now in the Screen orientation select Landscape and the directory name automatically
change to layout-land and let the Directory name as layout-land and don’t change it.
DDWD2713 – Mobile Programming 21
• Systems write info about views to Bundle other (app-specific) information must be saved by
programmer
o E.g. board state in a board game such as mastermind
• When Activity recreated Bundle sent to onCreate and onRestoreInstanceState()
• Can use either method to restore state data/ instance variables
• Since rotation causes activity to be destroyed and new one created, values of variables lost
or reset
• To stop lost or reset values, save them using onSaveInstanceState() before activity is
destroyed
o E.g. called before portrait layout is destroyed
• System calls onSaveInstanceState before onPause( ), onStop( ) and onDestroy( )
• For example, if we want to save the value of a variable mCurrentIndex during rotation
• First, create a constant as a key for storing data in the bundle
• Then override onSaveInstanceState() method to store the data
• Override OnRestoreInstanceState() method to retrieve the data
DDWD2713 – Mobile Programming 24
Creating UI programmatically
You can create user interface components such as layouts at runtime programmatically
1. First, create a LinearLayout object to contain all the views in the activity.
1. //Create a layout---------------
2. LinearLayout linearLayout = new LinearLayout(this);
3. linearLayout.setOrientation(LinearLayout.VERTICAL);
2. In this example, the activity will not be loaded from the main.xml file, as it is by default in
any Android Application.
First, we created a LayoutParams object to specify the layout parameter, which can be used
by other views.
3. Now, you need to create some UI elements like EditText, TextView, Button, CheckBox,
RadioGroup and RadioButton etc.
1. //----Create a Button------
2. Button myButton = new Button(this);
3. myButton.setText( "Press Me");
4. myButton.setBackgroundColor(Color.CYAN);
5. myButton.setPadding(20, 20, 20, 20);
6.
7. //----Create a TextView------
8. TextView myText = new TextView(this);
9. myText.setText("I am a TextView");
10. myText.setTextSize(20);
11. myText.setBackgroundColor(Color.MAGENTA);
12.
13. //----Create an EditText------
14. EditText myInput = new EditText(this);
15. myInput.setHint("I am an EditText");
16. myInput.setBackgroundColor(Color.YELLOW);
DDWD2713 – Mobile Programming 26
17. myInput.setWidth(layoutParams.WRAP_CONTENT);
All the elements created above are added to the LinearLayout object.
1. //---Add all elements to the layout
2. linearLayout.addView(myButton,layoutParams);
3. linearLayout.addView(myText,layoutParams);
4. linearLayout.addView(myInput);
References
1. Android Nerd Ranch, 1st edition
2. Busy Coder’s guide to AŶdroid versioŶ 4.4
3. CS 65/165 slides, Dartmouth College, Spring 2014
4. CS 371M slides, U of Texas Austin, Spring 2014
5. Slides from Prof. Emmanuel Agu, Worcester Polytechnic Institute, CS 528, Mobile and
Ubiquitous Computing Class, Fall 2017
6. 2017 J.F. DiMarzio, Beginning Android Programming with Android Studio, 4th Edition,
Indianapolis, Indiana