Advertisement
Guest User

ActivityOne

a guest
Feb 5th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. package course.labs.activitylab;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12.  
  13. public class ActivityOne extends Activity {
  14.  
  15.     private static final String RESTART_KEY = "restart";
  16.     private static final String RESUME_KEY = "resume";
  17.     private static final String START_KEY = "start";
  18.     private static final String CREATE_KEY = "create";
  19.  
  20.     // String for LogCat documentation
  21.     private final static String TAG = "Lab-ActivityOne";
  22.    
  23.  
  24.     // Lifecycle counters  
  25.     // TODO:
  26.     // Create counter variables for onCreate(), onRestart(), onStart() and
  27.     // onResume(), called mCreate, etc.
  28.     protected int mCreate=0;
  29.     protected int mRestart=0;
  30.     protected int mStart=0;
  31.     protected int mResume=0;
  32.     // You will need to increment these variables' values when their
  33.     // corresponding lifecycle methods get called
  34.  
  35.  
  36.  
  37.     // TODO: Create variables for each of the TextViews, called
  38.         // mTvCreate, etc.
  39.     protected TextView mTvCreate;
  40.     protected TextView mTvRestart;
  41.     protected TextView mTvStart;
  42.     protected TextView mTvResume;
  43.  
  44.     @Override
  45.     protected void onCreate(Bundle savedInstanceState) {
  46.         super.onCreate(savedInstanceState);
  47.          
  48.         setContentView(R.layout.activity_one);
  49.        
  50.         // TODO: Assign the appropriate TextViews to the TextView variables
  51.         // Hint: Access the TextView by calling Activity's findViewById()
  52.          mTvCreate  = (TextView) findViewById(R.id.create);
  53.          mTvRestart = (TextView) findViewById(R.id.restart);
  54.          mTvStart = (TextView) findViewById(R.id.start);
  55.          mTvResume = (TextView) findViewById(R.id.resume);
  56.         Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
  57.         launchActivityTwoButton.setOnClickListener(new OnClickListener() {
  58.                
  59.                 @Override
  60.                 public void onClick(View v) {
  61.                     // TODO:
  62.                     // Launch Activity Two
  63.                     // Hint: use Context's startActivity() method
  64.                     // Create an intent stating which Activity you would like to start
  65.                       Intent myIntent = new Intent(ActivityOne.this,ActivityTwo.class);
  66.                     // Launch the Activity using the intent              
  67.                     startActivity(myIntent);  
  68.                 }
  69.             });
  70.            
  71.             // Check for previously saved state
  72.             if (savedInstanceState != null) {
  73.  
  74.                 // TODO:
  75.                 // Restore value of counters from saved state
  76.                 // Only need 4 lines of code, one for every count variable
  77.                 mCreate = savedInstanceState.getInt(CREATE_KEY);
  78.                 mStart = savedInstanceState.getInt(START_KEY);
  79.                 mResume = savedInstanceState.getInt(RESUME_KEY);
  80.                 mRestart = savedInstanceState.getInt(RESTART_KEY);
  81.            
  82.             }
  83.  
  84.             // TODO: Emit LogCat message
  85.             Log.i(TAG,CREATE_KEY);
  86.  
  87.             // TODO:
  88.             // Update the appropriate count variable
  89.             // Update the user interface via the displayCounts() method
  90.            
  91.             mCreate++;
  92.             displayCounts();
  93.  
  94.  
  95.         }
  96.  
  97.     // Lifecycle callback methods overrides
  98.  
  99.     @Override
  100.     public void onStart() {
  101.         super.onStart();
  102.  
  103.         // TODO: Emit LogCat message
  104.         Log.i(TAG, START_KEY);
  105.  
  106.         // TODO:
  107.         // Update the appropriate count variable
  108.         // Update the user interface
  109.         mStart++;
  110.         displayCounts();
  111.  
  112.  
  113.     }
  114.  
  115.     @Override
  116.     public void onResume() {
  117.         super.onResume();
  118.  
  119.         // TODO: Emit LogCat message
  120.         Log.i(TAG, RESUME_KEY);
  121.  
  122.         // TODO:
  123.         // Update the appropriate count variable
  124.         // Update the user interface
  125.         mResume++;
  126.         displayCounts();
  127.  
  128.  
  129.     }
  130.  
  131.     @Override
  132.     public void onPause() {
  133.         super.onPause();
  134.  
  135.         // TODO: Emit LogCat message
  136.         Log.i(TAG,"PAUSED");
  137.        
  138.  
  139.     }
  140.  
  141.     @Override
  142.     public void onStop() {
  143.         super.onStop();
  144.  
  145.         // TODO: Emit LogCat message
  146.         Log.i(TAG,"STOPPED");
  147.  
  148.  
  149.     }
  150.    
  151.     @Override
  152.     public void onRestart() {
  153.         super.onRestart();
  154.  
  155.         // TODO: Emit LogCat message
  156.         Log.i(TAG,RESTART_KEY);
  157.  
  158.         // TODO:
  159.         // Update the appropriate count variable
  160.         // Update the user interface
  161.         mRestart++;
  162.         displayCounts();
  163.  
  164.  
  165.     }
  166.  
  167.     @Override
  168.     public void onDestroy() {
  169.         super.onDestroy();
  170.  
  171.         // TODO: Emit LogCat message
  172.         Log.i(TAG,"DESTROYED");
  173.     }
  174.  
  175.     @Override
  176.     public void onSaveInstanceState(Bundle savedInstanceState) {
  177.  
  178.         // TODO:
  179.         // Save counter state information with a collection of key-value pairs
  180.         // 4 lines of code, one for every count variable
  181.         savedInstanceState.putInt(CREATE_KEY, mCreate);
  182.         savedInstanceState.putInt(START_KEY, mStart);
  183.         savedInstanceState.putInt(RESUME_KEY, mResume);
  184.         savedInstanceState.putInt(RESTART_KEY, mRestart);
  185.         Log.i(TAG, "Saved current state");
  186.    
  187.     }
  188.  
  189.     // Updates the displayed counters
  190.     public void displayCounts() {
  191.  
  192.         mTvCreate.setText("onCreate() calls: " + mCreate);
  193.         mTvStart.setText("onStart() calls: " + mStart);
  194.         mTvResume.setText("onResume() calls: " + mResume);
  195.         mTvRestart.setText("onRestart() calls: " + mRestart);
  196.    
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement