android页面切换和常用方法的执行顺序
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
|
package com.example.android.active;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
/** * @deprecated Activity生命周期中的7大方法和四中状态
* */
public class MainActivity extends Activity {
/**
* Activity被创建时调用
* 可以在该方法中初始化UI组件
* 该方法调用完毕会调用onStart()方法
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println( "MainActivity-onCreate()" );
}
/**
* 被onCreate()调用,此时activity课件,但不能被操作
* 该方法会调用onResume()或者onStop()方法
* */
@Override
protected void onStart() {
// TODO Auto-generated method stub
super .onStart();
System.out.println( "MainActivity-onStart()" );
}
/**
* 当一个处于stop状态下的activity重新获取焦点时调用
* 该方法会调用onStart方法
*
*
* */
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super .onRestart();
System.out.println( "MainActivity-onRestart()" );
}
/**
* 被onStart()调用,此时activity可见可交互,
* 此时,activity处于**(Running)状态
*
* */
@Override
protected void onResume() {
super .onResume();
System.out.println( "MainActivity-onResume()" );
}
/**
* 当activity处于运行状态,被其他activity覆盖(位完全覆盖式调用)
* 此时activity处于暂停状态,失去焦点,不可操作
* 此状态下activity可以被kill掉
* 如果当用户返回该activity,那么会调用onResume方法,重新获取焦点
* */
@Override
protected void onPause() {
// TODO Auto-generated method stub
super .onPause();
System.out.println( "MainActivity-onPause()" );
}
/**
* 当activity完全被另一个activity覆盖时被调用
* 此时activity进入停止状态,完全不可见
* 此状态下activity可以被kill掉
* 当用户重新回到改activity,会调用restart方法
*
*
* */
@Override
protected void onStop() {
// TODO Auto-generated method stub
super .onStop();
System.out.println( "MainActivity-onStop()" );
}
/**
* 当activity调用了finish方法,或被系统销毁时被调用
* 可以在该方法中进行资源释放的工作
* */
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super .onDestroy();
System.out.println( "MainActivity-onDestory()" );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true ;
}
/**
* 通过这个方法跳转到activity2界面*/
public void gotoActivity2(View v){
//创建一个意图
Intent intent= new Intent( this ,MainActivity2. class );
//启动另一个activity
startActivity(intent);
}
} |
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
|
package com.example.android.active;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity2 extends Activity{
/**
* Activity被创建时调用
* 可以在该方法中初始化UI组件
* 该方法调用完毕会调用onStart()方法
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println( "MainActivity2-onCreate()" );
}
/**
* 被onCreate()调用,此时activity课件,但不能被操作
* 该方法会调用onResume()或者onStop()方法
* */
@Override
protected void onStart() {
// TODO Auto-generated method stub
super .onStart();
System.out.println( "MainActivity2-onStart()" );
}
/**
* 当一个处于stop状态下的activity重新获取焦点时调用
* 该方法会调用onStart方法
*
*
* */
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super .onRestart();
System.out.println( "MainActivity2-onRestart()" );
}
/**
* 被onStart()调用,此时activity可见可交互,
* 此时,activity处于**(Running)状态
*
* */
@Override
protected void onResume() {
super .onResume();
System.out.println( "MainActivity2-onResume()" );
}
/**
* 当activity处于运行状态,被其他activity覆盖(位完全覆盖式调用)
* 此时activity处于暂停状态,失去焦点,不可操作
* 此状态下activity可以被kill掉
* 如果当用户返回该activity,那么会调用onResume方法,重新获取焦点
* */
@Override
protected void onPause() {
// TODO Auto-generated method stub
super .onPause();
System.out.println( "MainActivity2-onPause()" );
}
/**
* 当activity完全被另一个activity覆盖时被调用
* 此时activity进入停止状态,完全不可见
* 此状态下activity可以被kill掉
* 当用户重新回到改activity,会调用restart方法
*
*
* */
@Override
protected void onStop() {
// TODO Auto-generated method stub
super .onStop();
System.out.println( "MainActivity2-onStop()" );
}
/**
* 当activity调用了finish方法,或被系统销毁时被调用
* 可以在该方法中进行资源释放的工作
* */
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super .onDestroy();
System.out.println( "MainActivity2S-onDestory()" );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true ;
}
} |
文件配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<application
android:allowBackup= "true"
android:icon= "@drawable/ic_launcher"
android:label= "@string/app_name"
android:theme= "@style/AppTheme" >
<activity
android:name= "com.example.android.active.MainActivity"
android:label= "@string/app_name" >
<intent-filter>
<action android:name= "android.intent.action.MAIN" />
<category android:name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name= ".MainActivity2" >
</activity>
</application>
|
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
|
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:paddingBottom= "@dimen/activity_vertical_margin"
android:paddingLeft= "@dimen/activity_horizontal_margin"
android:paddingRight= "@dimen/activity_horizontal_margin"
android:paddingTop= "@dimen/activity_vertical_margin"
tools:context= ".MainActivity" >
<TextView
android:id= "@+id/textView1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "@string/hello_world" />
<Button
android:id= "@+id/button1"
style= "?android:attr/buttonStyleSmall"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_below= "@+id/textView1"
android:layout_marginLeft= "15dp"
android:layout_marginTop= "32dp"
android:layout_toRightOf= "@+id/textView1"
android:text= "启动第二个activity" android:onClick= "gotoActivity2" />
</RelativeLayout> |
运行项目
点击启动第二个activity后
点击返回按钮后
本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1880918