SE 435: Development in the Android Environment Recitations 2 3 Semester 1 5779 4 Dec - 11 Dec 2018 Lifecycle Callbacks and Intents In this recitation we ll prepare a mockup tool which demonstrates the lifecycle of an Activity and shows how to use a very simple kind of Intent - an explicit intent. 1 Activity Lifecycle Callbacks As discussed in class, there are seven main callback functions in the lifecycle of an Activity: 1. oncreate 2. onstart 3. onresume 4. onpause 5. onrestart 6. onstop 7. ondestroy To better understand how the lifecycle callbacks are run and in what order, we ll make a simple app which prints out a notice whenever a lifecycle callback function is invoked. We ll perform the following steps: 1. Create an app with minimum SDK level 15 (to prevent bugs with the ActionBar). We ll override the API callbacks listed above (oncreate, onstart, onresume, onpause, onrestart, onstop, ondestroy) and print out a line in the log whenever they are called. For simplicity, we ll start with a simple logging function: private void logline(string line) { Log.i(LOG TAG, line); } This logging function prints a line which we can see in LogCat each time we call it. We ll just provide an appropriate line in each callback function. To make this work, we need to also define a variable called LOG TAG in the class: private final String LOG TAG = "Activity Lifecycle"; 2. We ll make the logging more interesting by adding a TextView on the main activity screen to show the log as well. After we add the TextView (called tveventlog), we can modify the logging function to be as follows: private void logline(string line) { Calendar c = Calendar.getInstance(); TextView tv = (TextView)findViewById(R.id.tvEventLog); tv.settext(c.get(calendar.hour_of_day) + ":" + c.get(calendar.minute) + ":" + c.get(calendar.second) + c.get(calendar.millisecond) + " " + line + "\n" + tv.gettext());; Log.i(LOG_TAG, line); } This function also prepends the log line and time for the event. 1
2 Adding Strings Translations Android coding conventions call for the storage of static strings in a file called strings.xml in the values folder of the app, rather than hard coded into the program. This has a number of advantages, primarily for portability. In this stage of the recitation we ll modify strings.xml file to contain strings for each line we want to log and replace all string literals in the code with references to the strings. To make the app more portable, we ll also create Hebrew versions of strings.xml which we will store in the values-iw folder. We can then change the language of the device and see that all strings are replaced consistently with their Hebrew translation. We can then see the Hebrew strings by changing the language of the device or emulator. Translating strings can be done more easily using the Translation Editor window in Android Studio as shown below. The editor allows you to prepare multiple translations for each string. In the figure below, there is Hebrew and Russian for each English string. 3 Adding Another Activity In the final part of the recitation, we ll add a second activity to the app (call it SecondActivity). The second activity will have a two Buttons and two TextViews. A screen shot is shown below: 2
The top button causes the activity to generate two random numbers which are displayed above. The bottom button causes the activity to run its finish function which closes it. We ll also add a Button to the top activity which lets us call the second activity. Clicking on the Button causes an explicit Intent to be created and sent, leading to the creation of the second activity. A final screen shot of the top activity is shown below: 3
Screen shots of the activities in Hebrew are shown below. Note that the text is shown in correct right to left, but the layout is a bit messed up. We could create separate layouts for the RTL languages to make sure that the Buttons and TextViews are shown in more logical locations. 4
5