Reference: 1,http://guides.codepath.com/android/Activity-Lifecycle
2, https://github.com/xxv/android-lifecycle
PartI : Activity 生命周期:
Lifecycle Method | Description | Common Uses |
---|---|---|
onCreate() | The activity is starting (but not visible to the user) | Most of the activity initialization code goes here. This is where you setContentView() for the activity, initialize views, set up any adapters, etc. |
onStart() | The activity is now visible (but not ready for user interaction) | This lifecycle method isn't used much, but can come in handy to register a BroadcastReceiver to monitor for changes that impact the UI (since the UI is now visible to the user). |
onResume() | The activity is now in the foreground and ready for user interaction | This is a good place to start animations, open exclusive-access devices like the camera, etc. |
onPause() | Counterpart to onResume() . The activity is about to go into the background and has stopped interacting with the user. This can happen when another activity is launched in front of the current activity. | It's common to undo anything that was done in onResume() and to save any global state (such as writing to a file). |
onStop() | Counterpart to onStart() . The activity is no longer visible to the user. | It's common to undo anything that was done inonStart() . |
onDestroy() | Counterpart to onCreate(...) . This can be triggered because finish() was called on the activity or the system needed to free up some memory. | It's common to do any cleanup here. For example, if the activity has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread here in onDestroy() |
onRestart() | Called when the activity has been stopped, before it is started again | It isn't very common to need to implement this callback. |
Part II :抽象Activity基类
http://blog.csdn.net/birdsaction/article/details/45873595
http://blog.csdn.net/u013045971/article/details/47418093
Part III: Activity的四种启动模式:
http://blog.csdn.NET/liuhe688/article/details/6754323/
1,standard ---------------- 默认的启动模式,不管Activity栈中是否有实例,都产生一个新的实例。
2,singleTop -------------- 如果发现在栈顶有对应的Activity实例,则重复利用,不再生成新的实例。
3,singleTask ------------- 如果在整个Activity栈中有有对应的Activity实例;则其他的Activity实例全部出栈,把所需的Activity实例设置成栈顶,显示到幕前。
4,singleInstance ---------- 启动一个新的栈结构。
PartIV: Activity ----- onSaveInstanceState()
1, 在一个Activity被销毁前,不一定会调用onSaveInstanceState();因为不是所有情况都需要存储Activity的状态(如用户按下返回按键时,用户的意图是关掉该Activity)
2,因此onSaveInstanceState()的调用不确定性;不适合被用于存储持久化数据(如保存数据到数据库);只应该用来记录acitivity 的瞬间状态(UI状态);
3,因为默认的onSaveInstanceState()方法帮助UI存储它的状态,所以,如果需要存储额外的状态,都需执行父类的onSaveInstanceState()方法;
4,Example:
public class MainActivity extends Activity {
public static final int SECOND_ACTIVITY = 0;
private String temp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 从savedInstanceState中恢复数据, 如果没有数据需要恢复savedInstanceState为null
if (savedInstanceState != null) {
temp = savedInstanceState.getString("temp");
System.out.println("onCreate: temp = " + temp);
}
}
public void onResume() {
super.onResume();
temp = "xing";
System.out.println("onResume: temp = " + temp);
// 切换屏幕方向会导致activity的摧毁和重建
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
System.out.println("屏幕切换");
}
}
// 将数据保存到outState对象中, 该对象会在重建activity时传递给onCreate方法
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("temp", temp);
}