Advertisement
Guest User

ActivityTwo

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