LessonExample
Android Activity State change handling example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
import android.content.res.Configuration; import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { public static final String TAG = "Android Activity State"; // view private EditText etString; private TextView tvOutput; // string to be saved private String myString; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first setContentView(R.layout.activity_main); bindView(); // No need to use this if we implement onRestoreInstanceState // Check whether we're recreating a previously destroyed instance // if (savedInstanceState != null) { // // Restore value of members from saved state // //myString = savedInstanceState.getString("mySavedString"); // Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]"); // Log.d(TAG, "onCreate() called with: myString = [" + myString + "]"); // } // else { // // Probably initialize members with default values for a new instance // myString = null; // Log.d(TAG, "onCreate() called with: myString = [" + myString + "]"); // } } private void bindView(){ etString = (EditText) findViewById(R.id.et_string); tvOutput = (TextView) findViewById(R.id.tv_output); } @Override protected void onSaveInstanceState(Bundle outState) { // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(outState); // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted. //Format: outState.putVarType("VarName", "VarValue"); outState.putString("mySavedString", myString); Log.d(TAG, "onSaveInstanceState() called with: outState = [" + outState + "]"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore UI state from the savedInstanceState. // This bundle has also been passed to onCreate. // Format: VarType varName = savedInstanceState.getVarType("VarName"); myString = savedInstanceState.getString("mySavedString"); Log.d(TAG, "onRestoreInstanceState() called with: savedInstanceState = [" + savedInstanceState + "]"); Log.d(TAG, "onRestoreInstanceState() called with: myString = [" + myString + "]"); } public void process(View view){ switch (view.getId()) { case R.id.btn_save: myString = etString.getText().toString(); etString.setText(""); etString.setHint("Type something here"); break; case R.id.btn_show: if (myString != null) tvOutput.setText(myString); else tvOutput.setText("No saved string."); break; case R.id.btn_clear: resetView(); break; } } private void resetView(){ etString.setText(""); etString.setHint("Type something here"); tvOutput.setText(""); } // called when the activity is about to become visible @Override protected void onStart() { super.onStart(); //Log.d(TAG, "onStart() called"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart() called"); } // called when the activity has become visible @Override protected void onResume() { super.onResume(); //resetView(); Log.d(TAG, "onResume() called"); } // called when another activity is taking focus @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause() called"); } // called when the activity is no longer visible @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop() called"); } // called just before the activity is destroyed // Configuration changes during runtime (screen orientation, keyboard availability, and language) also call onDestroy @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy() called"); } } |