fenlan

Everything gonna be fine in the end, if it's not fine, it's not the end.

0%

android生命周期函数记录

函数及解释如下

1
2
3
4
5
6
7
onCreate    在activity对象被第一次创建时调用
onStart 在activity变得可见时调用该函数
onResume 在activity开始准备与用户交互时调用该方法
onPause 当系统即将启动另外一个activity之前调用该方法
onStop 当前activity变得不可见时调用该方法
onDestroy 当前activity被销毁之前将会调用该方法
onRestroy 当一个activity再次启动之前将会调用该方法

来张图片帮助理解

one

提供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
package com.example.activitytest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private Button btn;
private static final String TAG = "ActivityTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "MainActivity onCreate");
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});

}
@Override
protected void onPause() {
Log.d(TAG, "MainActivity onPause ");
super.onPause();
}
@Override
protected void onResume() {
Log.d(TAG, "MainActivity onResume ");
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"MainActivity onStart ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "MainActivity onStop ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "MainActivity onDestroy ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "MainActivity onRestart ");
}
}