Thus far we have built and demonstrated simple applications
involving basic user
interfaces leveraging the Android Activity
component.
What we have not covered in these applications is the layout
orientation change. . In a real
device, one can
hold the device vertical or horizontal changing the orientation of the
application
user interface accordingly.
How do we simulation the orientation change in the Virtual Android
Device ? Once
the Virtual Android Device is launched, pressing either
CTRL+F11 or CTRL+F12
will change the orientation of the Virtual Android Device.
Screen orientation change will cause an Activity
to be
destroyed and recreated. This is because the screen orientation change
might need
to load alternative set of resources (such as the layout, images etc).
To offer a seemless user experience when the user changes the screen
orientation
in our Stock Ticker application, we need to persist the user interface
state (stock
prices) and restore it on orientation change. This is where we leverage
the Android Activity callback method
onSaveInstanceState() to persist
user interface state. This method is called before the callback method
onStop() and before the Activity
is destroyed.
We will now demonstrate the seemless user experience of screen
orientation change
by creating a new Android application named DroidStockTicker3.
This application is a modified version of the application DroidStockTicker2
leveraging the callback methods onSaveInstanceState()
, onStart(), and onStop().
Without further ado, lets get started. We will not go step-by-step
to show the various
screens since we already did that in Part-2 for the DroidTipCalculator
application.
The contents of the dimens.xml
file to look like the one shown in the
listing 9.1 below:
The contents of the strings.xml
file to look like the one shown in the
listing 9.2 below:
The contents of the main activity layout in file activity_stock_ticker.xml
will look like the one shown in the listing 9.3 below:
The contents of each list item layout in file list_item_row.xml
will look like the one shown in the listing 9.4 below:
The Java class TickSignal is shown
in the listing 9.5 below:
The Java class
StockTicker is shown
in the listing 9.6 below:
The Java class
TickerArrayAdapter is shown
in the listing 9.7 below:
Finally, we have the java source file corresponding to the main
stock ticker Activity called
StockTickerActivity.java.
The contents of the java source file StockTickerActivity.java
will look like the one shown in the listing 9.8 below:
From the code Listing 9.8 above, the following are some of the
interesting details:
In the onSaveInstanceState() method,
we persist
the current stock prices and store it in the Bundle
In the onCreate() method, we check
the passed
in Bundle to see if it is null. It is null
when
the application is newly launched and not null when the orientation
changes
In the OnStart() method, we create an
instance of TickerUpdateTask
object and invoke the execute()
method to start the AsyncTask
In the OnStop() method, we cancel the
current instance of TickerUpdateTask
In the doInBackground() method of the
class TickerUpdateTask, we check if the AsyncTask
is cancelled and exit accordingly. Point to note here - the onPostExecute() method will not be
called when an AsyncTask is cancelled
We are now ready to test our DroidStockTicker3
application on the virtual Android device we created in
Part-1.
We will create a Run Configuration for DroidStockTicker3
as we did in Part-2
for DroidTipCalculator.
Once the run configuration for DroidStockTicker3
is ready, we will Run
the application and the application will come to life as shown in the
following figure 9.1 below:
Figure-9.1
Wait about 10 seconds and the device screen will be refreshed as
shown in the
following figure 9.2 below:
Figure-9.2
Press CTRL+F12 to change the orientation
of the screen from vertical to horizontal and the virtual device screen
will
appear as shown in the following figure 9.3 below:
Figure-9.3
The following figure 9.4 below shows the output from the console
to give us an idea of what is happening behind the scenes:
Figure-9.4
An application can also fix the screen orientation to either
portrait or landscape.
There are many mobile apps that take this approach - Angry Birds being
one of the popular ones that has a horizontal orientation.
In order to fix the screen orientation to portrait
for our DroidStockTicker3 application, we
will modify
the AndroidManifest.xml to look like the one
shown
in the listing 9.9 below:
Restart the DroidStockTicker3 application
in the Virtual Android Device and press CTRL+F12
to change the orientation of the screen from vertical to horizontal and
the virtual device screen will
appear as shown in the following figure 9.5 below:
Figure-9.5
As can be observed, even though the virtual device is horizontal,
the
orientation of the DroidStockTicker3
application
is still vertical.
The following figure 9.6 below shows the output from the console
to give us an idea of what is happening behind the scenes when the
orientation is fixed at portrait: