-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
100 lines (71 loc) · 2.36 KB
/
MainActivity.java
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
package com.bentals.screenrotation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
//best practice TAG = class name (ActivityLifeCycle)
private final String TAG = getClass().getSimpleName();
private final String inputStateKey = "inputStateKey";
//views
private EditText input;
//Two Activity life cycle function that are important for screen rotation
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate: " + savedInstanceState);
//init views
input = (EditText) findViewById(R.id.input);
//check if we have a bundle
if (savedInstanceState != null) {
//screen was rotated or home button was pressed
//(or any other action that moved the app in to the background)
//and we saved some values in the savedInstanceState bundle
//get saved value
String savedText = savedInstanceState.getString(inputStateKey);
input.setText(savedText);
} else {
//the first time the app is launched or no state was saved in screen rotation.`
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
//save input text in to the state bundle
String inputText = input.getText().toString();
outState.putString(inputStateKey, inputText);
}
//The rest od the activities life cycle
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart");
}
}