package com.example.android.lifecycle;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    /*
     * This tag will be used for logging. It is best practice to use the class's name using
     * getSimpleName as that will greatly help to identify the location from which your logs are
     * being posted.
     */
    private static final String TAG = MainActivity.class.getSimpleName();

    /* Constant values for the names of each respective lifecycle callback */
    private static final String ON_CREATE = "onCreate";
    private static final String ON_START = "onStart";
    private static final String ON_RESUME = "onResume";
    private static final String ON_PAUSE = "onPause";
    private static final String ON_STOP = "onStop";
    private static final String ON_RESTART = "onRestart";
    private static final String ON_DESTROY = "onDestroy";
    private static final String ON_SAVE_INSTANCE_STATE = "onSaveInstanceState";

    /*
     * This TextView will contain a running log of every lifecycle callback method called from this
     * Activity. This TextView can be reset to its default state by clicking the Button labeled
     * "Reset Log"
     */
    private TextView mLifecycleDisplay;

    /**
     * Called when the activity is first created. This is where you should do all of your normal
     * static set up: create views, bind data to lists, etc.
     *
     * Always followed by onStart().
     *
     * @param savedInstanceState The Activity's previously frozen state, if there was one.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLifecycleDisplay = (TextView) findViewById(R.id.tv_lifecycle_events_display);

        // TODO (1) Use logAndAppend within onCreate
    }

    // TODO (2) Override onStart, call super.onStart, and call logAndAppend with ON_START

    // TODO (3) Override onResume, call super.onResume, and call logAndAppend with ON_RESUME

    // TODO (4) Override onPause, call super.onPause, and call logAndAppend with ON_PAUSE

    // TODO (5) Override onStop, call super.onStop, and call logAndAppend with ON_STOP

    // TODO (6) Override onRestart, call super.onRestart, and call logAndAppend with ON_RESTART

    // TODO (7) Override onDestroy, call super.onDestroy, and call logAndAppend with ON_DESTROY
    /**
     * The final call you receive before your activity is destroyed. This can happen either because
     * the activity is finishing (someone called finish() on it, or because the system is
     * temporarily destroying this instance of the activity to save space. You can distinguish
     * between these two scenarios with the isFinishing() method.
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();

        logAndAppend(ON_DESTROY);
    }

    /**
     * Logs to the console and appends the lifecycle method name to the TextView so that you can
     * view the series of method callbacks that are called both from the app and from within
     * Android Studio's Logcat.
     *
     * @param lifecycleEvent The name of the event to be logged.
     */
    private void logAndAppend(String lifecycleEvent) {
        Log.d(TAG, "Lifecycle Event: " + lifecycleEvent);

        mLifecycleDisplay.append(lifecycleEvent + "\n");
    }

    /**
     * This method resets the contents of the TextView to its default text of "Lifecycle callbacks"
     *
     * @param view The View that was clicked. In this case, it is the Button from our layout.
     */
    public void resetLifecycleDisplay(View view) {
        mLifecycleDisplay.setText("Lifecycle callbacks:\n");
    }
}

@@ -1945,98 +1945,92 @@
     
-// TODO (1) Use logAndAppend within onCreate%0A    %7D%0A%0A    // TODO (2) Override onStart, call
+logAndAppend(ON_CREATE);%0A    %7D%0A%0A    @Override%0A    protected void onStart() %7B%0A       
  sup
@@ -2035,34 +2035,35 @@
 uper.onStart
-, and call
+();%0A       
  logAndAppen
@@ -2067,22 +2067,17 @@
 pend
- with 
+(
 ON_START
 %0A%0A  
@@ -2076,49 +2076,71 @@
 TART
-%0A
+);
 %0A    
-// TODO (3) Override onResume, call
+%7D%0A%0A    @Override%0A    protected void onResume() %7B%0A       
  sup
@@ -2150,26 +2150,27 @@
 onResume
-, and call
+();%0A       
  logAndA
@@ -2170,30 +2170,25 @@
 logAndAppend
- with 
+(
 ON_RESUME%0A%0A 
@@ -2188,48 +2188,70 @@
 SUME
-%0A
+);
 %0A    
-// TODO (4) Override onPause, call
+%7D%0A%0A    @Override%0A    protected void onPause() %7B%0A       
  sup
@@ -2260,26 +2260,27 @@
 .onPause
-, and call
+();%0A       
  logAndA
@@ -2288,22 +2288,17 @@
 pend
- with 
+(
 ON_PAUSE
 %0A%0A  
@@ -2297,47 +2297,69 @@
 AUSE
-%0A
+);
 %0A    
-// TODO (5) Override onStop, call
+%7D%0A%0A    @Override%0A    protected void onStop() %7B%0A       
  sup
@@ -2367,26 +2367,27 @@
 r.onStop
-, and call
+();%0A       
  logAndA
@@ -2395,160 +2395,80 @@
 pend
- with 
+(
 ON_STOP
-%0A
+);
 %0A    
-// TODO (6) Override onRestart, call super.onRestart, and call logAndAppend with ON_RESTART%0A%0A    // TODO (7) Override onDestroy, call
+%7D%0A%0A    @Override%0A    protected void onRestart() %7B%0A       
  sup
@@ -2476,25 +2476,26 @@
 r.on
-Destroy, and call
+Restart();%0A       
  log
@@ -2507,24 +2507,28 @@
 pend
- with ON_DESTROY
+(ON_RESTART);%0A    %7D%0A
 %0A   

Solve log lifecycle