Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Last active August 29, 2015 14:27
Show Gist options
  • Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
Save clamytoe/51343bfafc7eb2580acd to your computer and use it in GitHub Desktop.
HKUSTx: COMP107x Introduction to Mobile Application Development using Android
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise: Greet Friend (with Lifecycle)\n",
"---\n",
"## Objectives\n",
"\n",
"Our endeavour in this exercise is to understand Android activity lifecycle methods. At the end of this exercise, you will:\n",
"\n",
"* Be familiar with the various lifecycle methods of an Android activity\n",
"* Be familiar with the use of Logcat and Log messages to track the progress of an app execution\n",
"\n",
"\n",
"## Introduction\n",
"\n",
"In this exercise, we will modify the **Greet Friend (with ListActivity)** exercise by adding in the lifecycle methods for both the MainActivity and the ShowMessage activities. We will make use of Logcat to track how these methods are invoked by the Android framework as the application runs. The real use of these lifecycle methods will become clear in one of the later exercises.\n",
"\n",
"## Textual Instructions\n",
"---\n",
"### Step 1: Adding Activity Lifecycle Methods\n",
"\n",
"1. Modify the **MainActivity.java** file by adding in various lifecycle methods for the activity as shown below.\n",
"\n",
" String [] names;\n",
"\n",
" private static final String TAG = \"MainActivity\";\n",
"\n",
" @Override\n",
" protected void onCreate(Bundle savedInstanceState) {\n",
" super.onCreate(savedInstanceState);\n",
"\n",
" // Construct a string array from the String Array resource \"friends\" in the\n",
" // strings.xml file\n",
" names = getResources().getStringArray(R.array.friends);\n",
"\n",
" // This method call sets the names string array as the source of the names\n",
" // for the list of items in the listview. The ArrayAdapter object is used\n",
" // to adapt the string array and construct a list of layout items that are\n",
" // then used by the ListView of the ListActivity to construct the list of friends.\n",
" // The layout of each item is specified by the friend_item.xml file\n",
" setListAdapter( new ArrayAdapter<String>(this, R.layout.friend_item, names));\n",
"\n",
" Log.i(TAG, \"in onCreate()\");\n",
"\n",
" }\n",
"\n",
" @Override\n",
" protected void onStart() {\n",
" super.onStart();\n",
"\n",
" Log.i(TAG, \"in onStart()\");\n",
"\n",
" }\n",
"\n",
" @Override\n",
" protected void onRestart() {\n",
" super.onRestart();\n",
" Log.i(TAG, \"in onRestart()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onResume() {\n",
" super.onResume();\n",
" Log.i(TAG, \"in onResume()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onPause() {\n",
" super.onPause();\n",
" Log.i(TAG, \"in onPause()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onStop() {\n",
" super.onStop();\n",
" Log.i(TAG, \"in onStop()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onDestroy() {\n",
" super.onDestroy();\n",
" Log.i(TAG, \"in onDestroy()\");\n",
" }\n",
" \n",
"2. Similarly, modify the **ShowMessage.java** file to add the lifecycle methods to this activity as shown below.\n",
"\n",
" private static final String TAG = \"ShowMessage\";\n",
"\n",
" @Override\n",
" protected void onCreate(Bundle savedInstanceState) {\n",
" super.onCreate(savedInstanceState);\n",
" setContentView(R.layout.activity_show_message);\n",
"\n",
" // We first get a reference to the Intent that resulted in this activity\n",
" // being started by the Android framework.\n",
" Intent in = getIntent();\n",
"\n",
" // From the Intent we retrieve the message that was sent from MainActivity\n",
" // Note the use of the same key, \"message\", to retrieve the message\n",
" String message = in.getStringExtra(\"message\");\n",
"\n",
" // Get the reference to the TextView on the ShowMessage UI\n",
" TextView textMessage = (TextView) findViewById(R.id.textMessage);\n",
"\n",
" // set the text of the TextView to display the incoming greeting message\n",
" textMessage.setText(message);\n",
"\n",
" Log.i(TAG, \"in onCreate()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onStart() {\n",
" super.onStart();\n",
"\n",
" Log.i(TAG, \"in onStart()\");\n",
"\n",
" }\n",
"\n",
" @Override\n",
" protected void onRestart() {\n",
" super.onRestart();\n",
" Log.i(TAG, \"in onRestart()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onResume() {\n",
" super.onResume();\n",
" Log.i(TAG, \"in onResume()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onPause() {\n",
" super.onPause();\n",
" Log.i(TAG, \"in onPause()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onStop() {\n",
" super.onStop();\n",
" Log.i(TAG, \"in onStop()\");\n",
" }\n",
"\n",
" @Override\n",
" protected void onDestroy() {\n",
" super.onDestroy();\n",
" Log.i(TAG, \"in onDestroy()\");\n",
" }\n",
"\n",
"3. After updating both the activities, run the application to see how the lifecycle methods of each activity are invoked by the Android framework as the app starts. Then move from MainActivity to ShowMessage. See the messages being printed out in the Logcat window."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment