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();

    // TODO (1) Create a key String called LIFECYCLE_CALLBACKS_TEXT_KEY

    /* 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 (6) If savedInstanceState is not null and contains LIFECYCLE_CALLBACKS_TEXT_KEY, set that text on our TextView

        logAndAppend(ON_CREATE);
    }

    /**
     * Called when the activity is becoming visible to the user.
     *
     * Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes
     * hidden.
     */
    @Override
    protected void onStart() {
        super.onStart();

        logAndAppend(ON_START);
    }

    /**
     * Called when the activity will start interacting with the user. At this point your activity
     * is at the top of the activity stack, with user input going to it.
     *
     * Always followed by onPause().
     */
    @Override
    protected void onResume() {
        super.onResume();

        logAndAppend(ON_RESUME);
    }

    /**
     * Called when the system is about to start resuming a previous activity. This is typically
     * used to commit unsaved changes to persistent data, stop animations and other things that may
     * be consuming CPU, etc. Implementations of this method must be very quick because the next
     * activity will not be resumed until this method returns.
     *
     * Followed by either onResume() if the activity returns back to the front, or onStop() if it
     * becomes invisible to the user.
     */
    @Override
    protected void onPause() {
        super.onPause();

        logAndAppend(ON_PAUSE);
    }

    /**
     * Called when the activity is no longer visible to the user, because another activity has been
     * resumed and is covering this one. This may happen either because a new activity is being
     * started, an existing one is being brought in front of this one, or this one is being
     * destroyed.
     *
     * Followed by either onRestart() if this activity is coming back to interact with the user, or
     * onDestroy() if this activity is going away.
     */
    @Override
    protected void onStop() {
        super.onStop();

        logAndAppend(ON_STOP);
    }

    /**
     * Called after your activity has been stopped, prior to it being started again.
     *
     * Always followed by onStart()
     */
    @Override
    protected void onRestart() {
        super.onRestart();

        logAndAppend(ON_RESTART);
    }
    
    /**
     * 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);
    }

    // TODO (2) Override onSaveInstanceState
    // Do steps 3 - 5 within onSaveInstanceState
    // TODO (3) Call super.onSaveInstanceState
    // TODO (4) Call logAndAppend with the ON_SAVE_INSTANCE_STATE String
    // TODO (5) Put the text from the TextView in the outState bundle

    /**
     * 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");
    }
}

@@ -555,46 +555,35 @@
     
-// TODO (1) Create a key String called
+private static final String
  LIF
@@ -607,16 +607,31 @@
 TEXT_KEY
+ = %22callbacks%22;
 %0A%0A    /*
@@ -2022,23 +2022,12 @@
     
-// TODO (6) If 
+if (
 save
@@ -2045,32 +2045,63 @@
 ate 
-is not null and 
+!= null && savedInstanceState%0A                .
 contains
  LIF
@@ -2096,17 +2096,20 @@
 contains
- 
+Key(
 LIFECYCL
@@ -2132,39 +2132,142 @@
 _KEY
-, set that text on our TextView
+)) %7B%0A            mLifecycleDisplay.setText(savedInstanceState%0A                    .getString(LIFECYCLE_CALLBACKS_TEXT_KEY));%0A        %7D
 %0A%0A  
@@ -4925,118 +4925,83 @@
     
-// TODO (2) Override onSaveInstanceState%0A    // Do steps 3 - 5 within onSaveInstanceState%0A    // TODO (3) Call
+@Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A       
  sup
@@ -5026,29 +5026,27 @@
 tate
-%0A    // TODO (4) Call
+(outState);%0A       
  log
@@ -5054,26 +5054,17 @@
 ndAppend
- with the 
+(
 ON_SAVE_
@@ -5081,85 +5081,130 @@
 TATE
- String%0A    // TODO (5) Put the text from the TextView in the outState bundle
+);%0A        outState.putString(LIFECYCLE_CALLBACKS_TEXT_KEY,%0A                mLifecycleDisplay.getText().toString());%0A    %7D
 %0A%0A  

Solve persist data