From 339c57d3520c8de6f970b020c87f77ec266f3bf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 12 Feb 2014 21:34:19 +0100 Subject: Android : 01-TheActivityClass: implement --- .../src/course/labs/activitylab/ActivityOne.java | 79 +++++++++++----------- .../src/course/labs/activitylab/ActivityTwo.java | 75 ++++++++++---------- 2 files changed, 76 insertions(+), 78 deletions(-) diff --git a/Android/01-TheActivityClass/ActivityLab/src/course/labs/activitylab/ActivityOne.java b/Android/01-TheActivityClass/ActivityLab/src/course/labs/activitylab/ActivityOne.java index 83e1df7..b5c7f01 100644 --- a/Android/01-TheActivityClass/ActivityLab/src/course/labs/activitylab/ActivityOne.java +++ b/Android/01-TheActivityClass/ActivityLab/src/course/labs/activitylab/ActivityOne.java @@ -21,48 +21,48 @@ public class ActivityOne extends Activity { // Lifecycle counters - // TODO: - // Create counter variables for onCreate(), onRestart(), onStart() and - // onResume(), called mCreate, etc. - // You will need to increment these variables' values when their - // corresponding lifecycle methods get called + private int mCreate; + private int mStart; + private int mResume; + private int mRestart; - // TODO: Create variables for each of the TextViews, called - // mTvCreate, etc. + private TextView mTvCreate; + private TextView mTvStart; + private TextView mTvResume; + private TextView mTvRestart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_one); - // TODO: Assign the appropriate TextViews to the TextView variables - // Hint: Access the TextView by calling Activity's findViewById() - // textView1 = (TextView) findViewById(R.id.textView1); + mTvCreate = (TextView) findViewById(R.id.create); + mTvStart = (TextView) findViewById(R.id.start); + mTvResume = (TextView) findViewById(R.id.resume); + mTvRestart = (TextView) findViewById(R.id.restart); Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); launchActivityTwoButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - // TODO: - // Launch Activity Two - // Hint: use Context's startActivity() method - // Create an intent stating which Activity you would like to start - // Launch the Activity using the intent + Intent intent = new Intent(ActivityOne.this, ActivityTwo.class); + startActivity(intent); } }); // Check for previously saved state if (savedInstanceState != null) { - // TODO: - // Restore value of counters from saved state - // Only need 4 lines of code, one for every count variable + Log.i(TAG, "restore savedInstanceState"); + mCreate = (Integer) savedInstanceState.getSerializable("mCreate"); + mStart = (Integer) savedInstanceState.getSerializable("mStart"); + mResume = (Integer) savedInstanceState.getSerializable("mResume"); + mRestart = (Integer) savedInstanceState.getSerializable("mRestart"); } - // TODO: Emit LogCat message - // TODO: - // Update the appropriate count variable - // Update the user interface via the displayCounts() method + Log.i(TAG, "onCreate()"); + mCreate += 1; + displayCounts(); } // Lifecycle callback overrides @@ -70,53 +70,52 @@ public class ActivityOne extends Activity { @Override public void onStart() { super.onStart(); - // TODO: Emit LogCat message - // TODO: - // Update the appropriate count variable - // Update the user interface + Log.i(TAG, "onStart()"); + mStart += 1; + displayCounts(); } @Override public void onResume() { super.onResume(); - // TODO: Emit LogCat message - // TODO: - // Update the appropriate count variable - // Update the user interface + Log.i(TAG, "onResume()"); + mResume += 1; + displayCounts(); } @Override public void onPause() { super.onPause(); - // TODO: Emit LogCat message + Log.i(TAG, "onPause()"); } @Override public void onStop() { super.onStop(); - // TODO: Emit LogCat message + Log.i(TAG, "onStop()"); } @Override public void onRestart() { super.onRestart(); - // TODO: Emit LogCat message - // TODO: - // Update the appropriate count variable - // Update the user interface + Log.i(TAG, "onRestart()"); + mRestart += 1; + displayCounts(); } @Override public void onDestroy() { super.onDestroy(); - // TODO: Emit LogCat message + Log.i(TAG, "onDestroy()"); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { - // TODO: - // Save state information with a collection of key-value pairs - // 4 lines of code, one for every count variable + Log.i(TAG, "onSaveInstanceState()"); + savedInstanceState.putSerializable("mCreate", mCreate); + savedInstanceState.putSerializable("mStart", mStart); + savedInstanceState.putSerializable("mResume", mResume); + savedInstanceState.putSerializable("mRestart", mRestart); } // Updates the displayed counters diff --git a/Android/01-TheActivityClass/ActivityLab/src/course/labs/activitylab/ActivityTwo.java b/Android/01-TheActivityClass/ActivityLab/src/course/labs/activitylab/ActivityTwo.java index edd3951..f210662 100644 --- a/Android/01-TheActivityClass/ActivityLab/src/course/labs/activitylab/ActivityTwo.java +++ b/Android/01-TheActivityClass/ActivityLab/src/course/labs/activitylab/ActivityTwo.java @@ -20,46 +20,46 @@ public class ActivityTwo extends Activity { // Lifecycle counters - // TODO: - // Create counter variables for onCreate(), onRestart(), onStart() and - // onResume(), called mCreate, etc. - // You will need to increment these variables' values when their - // corresponding lifecycle methods get called + private int mCreate; + private int mStart; + private int mResume; + private int mRestart; - // TODO: Create variables for each of the TextViews, called - // mTvCreate, etc. + private TextView mTvCreate; + private TextView mTvStart; + private TextView mTvResume; + private TextView mTvRestart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); - // TODO: Assign the appropriate TextViews to the TextView variables - // Hint: Access the TextView by calling Activity's findViewById() - // textView1 = (TextView) findViewById(R.id.textView1); + mTvCreate = (TextView) findViewById(R.id.create); + mTvStart = (TextView) findViewById(R.id.start); + mTvResume = (TextView) findViewById(R.id.resume); + mTvRestart = (TextView) findViewById(R.id.restart); Button closeButton = (Button) findViewById(R.id.bClose); closeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - // TODO: - // This function closes Activity Two - // Hint: use Context's finish() method + finish(); } }); // Check for previously saved state if (savedInstanceState != null) { - // TODO: - // Restore value of counters from saved state - // Only need 4 lines of code, one for every count variable + mCreate = (Integer) savedInstanceState.getSerializable("mCreate"); + mStart = (Integer) savedInstanceState.getSerializable("mStart"); + mResume = (Integer) savedInstanceState.getSerializable("mResume"); + mRestart = (Integer) savedInstanceState.getSerializable("mRestart"); } - // TODO: Emit LogCat message - // TODO: - // Update the appropriate count variable - // Update the user interface via the displayCounts() method + Log.i(TAG, "onCreate()"); + mCreate += 1; + displayCounts(); } // Lifecycle callback methods overrides @@ -67,53 +67,52 @@ public class ActivityTwo extends Activity { @Override public void onStart() { super.onStart(); - // TODO: Emit LogCat message - // TODO: - // Update the appropriate count variable - // Update the user interface + Log.i(TAG, "onStart()"); + mStart += 1; + displayCounts(); } @Override public void onResume() { super.onResume(); - // TODO: Emit LogCat message - // TODO: - // Update the appropriate count variable - // Update the user interface + Log.i(TAG, "onResume()"); + mResume += 1; + displayCounts(); } @Override public void onPause() { super.onPause(); - // TODO: Emit LogCat message + Log.i(TAG, "onPause()"); } @Override public void onStop() { super.onStop(); - // TODO: Emit LogCat message + Log.i(TAG, "onStop()"); } @Override public void onRestart() { super.onRestart(); - // TODO: Emit LogCat message - // TODO: - // Update the appropriate count variable - // Update the user interface + Log.i(TAG, "onRestart()"); + mRestart += 1; + displayCounts(); } @Override public void onDestroy() { super.onDestroy(); - // TODO: Emit LogCat message + Log.i(TAG, "onDestroy()"); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { - // TODO: - // Save counter state information with a collection of key-value pairs - // 4 lines of code, one for every count variable + Log.i(TAG, "onSaveInstanceState)"); + savedInstanceState.putSerializable("mCreate", mCreate); + savedInstanceState.putSerializable("mStart", mStart); + savedInstanceState.putSerializable("mResume", mResume); + savedInstanceState.putSerializable("mRestart", mRestart); } // Updates the displayed counters -- cgit v1.1-2-g2b99