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
|
< 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 = "com.example.activity_05.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"
???????? android:layout_width = "fill_parent"
???????? android:layout_height = "wrap_content"
???????? android:layout_below = "@+id/textView1"
???????? android:layout_marginTop = "22dp"
???????? android:text = "启动第二个Activity" ?/>
?
</ RelativeLayout >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? xml ?version = "1.0" ?encoding = "utf-8" ?>
< LinearLayout ?xmlns:android = "http://schemas.android.com/apk/res/android"
???? android:layout_width = "match_parent"
???? android:layout_height = "match_parent"
???? android:orientation = "vertical" ?>
?
???? < TextView
???????? android:id = "@+id/textView1"
???????? android:layout_width = "fill_parent"
???????? android:layout_height = "wrap_content"
???????? android:text = "This?is?the?second?activity" ?/>
?
</ LinearLayout >
|
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
|
package ?com.example.activity_05;
?
import ?android.support.v7.app.ActionBarActivity;
import ?android.content.Intent;
import ?android.os.Bundle;
import ?android.view.Menu;
import ?android.view.MenuItem;
import ?android.view.View;
import ?android.widget.Button;
import ?android.view.View.OnClickListener;
?
public ?class ?MainActivity? extends ?ActionBarActivity?{
???? private ?Button?button;
?
???? @Override
???? protected ?void ?onCreate(Bundle?savedInstanceState)?{
???????? super .onCreate(savedInstanceState);
???????? System.out.println( "MainActivity?onCreate:第一次调用Activity的时候执行" );
???????? setContentView(R.layout.activity_main);
???????? button?=?(Button)?findViewById(R.id.button1);
???????? button.setOnClickListener( new ?OnClickListener()?{
???????????? ????????????? @Override
???????????? public ?void ?onClick(View?v)?{
???????????????? Intent?intent?=? new ?Intent();
???????????????? intent.setClass(MainActivity. this ,?SecondActivity. class );
???????????????? MainActivity. this .startActivity(intent);
???????????? }
???????? });
???? }
???? ????? @Override
???? protected ?void ?onResume()?{
???????? System.out.println( "MainActivity?onResume" );
???????? super .onResume();
???? }
???? ???????? //以上三个方法执行完毕之后这个Activity就可以使用了?
???? ????? @Override
???? protected ?void ?onStart()?{
????????? System.out.println( "MainActivity?onStart:Activity处于可见状态的时候调用" );
???????? super .onStart();
???? }
???? ????? @Override
???? protected ?void ?onRestart()?{
???????? System.out.println( "MainActivity?onRestart:Activity又可以得到用户焦点的时候调用" );
???????? System.out.println( "当从第二个Activity又回到第一个Activity时调用此方法" );
???????? System.out.println( "然后调用onResume方法就可以显示出来" );
???????? System.out.println( "然后调用第二个Activity的onStop、onDestroy。第二个Activity就被销毁掉了" );
???????? super .onRestart();
???? }
???? ????? @Override
???? protected ?void ?onPause()?{
???????? System.out.println( "MainActivity?onPause" );
???????? System.out.println( "如果在执行当前Activity的时候又启动了一个Activity就会调用当前Activity的此方法" );
???????? System.out.println( "然后依次调用新启动的Activity的onCreate、onStart、onResume" );
???????? System.out.println( "然后调用第一个Activity的onStop方法" );
???????? super .onPause();
???? }
???? ????? @Override
???? protected ?void ?onStop()?{
???????? System.out.println( "MainActivity?onStop" );
???????? //如果第二个Activity把第一个Activity完全遮挡住就调用这个函数
???????? super .onStop();
???? }
???? ????? @Override
???? protected ?void ?onDestroy()?{
???????? System.out.println( "MainActivity?onDestroy" );
???????? super .onDestroy();
???? }
} |
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
|
package ?com.example.activity_05;
?
import ?android.app.Activity;
import ?android.os.Bundle;
?
public ?class ?SecondActivity? extends ?Activity?{
???? @Override
???? protected ?void ?onCreate(Bundle?savedInstanceState)?{
???????? super .onCreate(savedInstanceState);
???????? setContentView(R.layout.second);
???? }
???? ????? @Override
???? protected ?void ?onStart()?{
???????? System.out.println( "SecondActivity?onStart" );
???????? super .onStart();
???? }
???? ????? @Override
???? protected ?void ?onRestart()?{
???????? System.out.println( "SecondActivity?onRestart" );
???????? super .onRestart();
???? }
???? ????? @Override
???? protected ?void ?onResume()?{
???????? System.out.println( "SecondActivity?onResume" );
???????? super .onResume();
???? }
???? ????? @Override
???? protected ?void ?onPause()?{
???????? System.out.println( "SecondActivity?onPause" );
???????? super .onPause();
???? }
???? ????? @Override
???? protected ?void ?onStop()?{
???????? System.out.println( "SecondActivity?onStop" );
???????? super .onStop();
???? }
???? ????? @Override
???? protected ?void ?onDestroy()?{
???????? System.out.println( "SecondActivity?onDestroy" );
???????? super .onDestroy();
???? }
} |
1
|
< activity ?android:name = ".SecondActivity" ?android:label = "第二个Activity" />
|
?
原文:http://jlins.iteye.com/blog/2157163