http://peiquan.blog.51cto.com/7518552/1277373
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
|
public
class
ExampleActivity
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
// The activity is being created.
}
@Override
protected
void
onStart() {
super
.onStart();
// The activity is about to become visible.
}
@Override
protected
void
onResume() {
super
.onResume();
// The activity has become visible (it is now "resumed").
}
@Override
protected
void
onPause() {
super
.onPause();
// Another activity is taking focus (this activity is about to be "paused").
}
@Override
protected
void
onStop() {
super
.onStop();
// The activity is no longer visible (it is now "stopped")
}
@Override
protected
void
onDestroy() {
super
.onDestroy();
// The activity is about to be destroyed.
}
}
|
|
|||||
|
|||||
会在执行
onStoped()
方法之前调用,这个方法有一个参数
Bundle
,可以以“名称
-
值”的形式保存
activity
的信息(如使用
putString(),putInt()
方法)。接着在需要还原
activity
时,系统会调用
onCreat()
或者
OnRestoreInstanceState(),这两个方法都传入一个以保存了activity信息的Bundle对象,通过提取Bundle对象的信息来恢复activity。如果没有信息需要保存到Bundle对象,那传递给这两个方法的将是空的Bundle对象(刚开始初始化一个activity时其实就是这种情况)。如下图所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Override
protected
void
OnSaveInstanceState(Bundle outState)
{
outState.PutString(
"myString"
,
"HelloXamarin.Android OnSaveInstanceState"
);
outState.PutBoolean(
"myBool"
,
true
);
base.OnSaveInstanceState(outState);
}
@Override
protected
void
OnRestoreInstanceState(Bundle savedState)
{
base.OnRestoreSaveInstanceState(savedState);
var myString =savedState.GetString(amyStringa);
var myBool =GetBoolean(amyBoola);
}
|
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
|
public
class
ActivityA
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
System.out.println(
"ActivityA-->onCreate()"
);
}
@Override
protected
void
onStart() {
super
.onStart();
System.out.println(
"ActivityA-->onStart()"
);
}
@Override
protected
void
onRestart() {
super
.onRestart();
System.out.println(
"ActivityA-->onRestart()"
);
}
@Override
protected
void
onResume() {
super
.onResume();
System.out.println(
"ActivityA-->onResume()"
);
}
@Override
protected
void
onPause() {
super
.onPause();
System.out.println(
"ActivityA-->onPause()"
);
}
@Override
protected
void
onStop() {
super
.onStop();
System.out.println(
"ActivityA-->onStop()"
);
}
@Override
protected
void
onDestroy() {
super
.onDestroy();
System.out.println(
"ActivityA-->onDestroy()"
);
}
public
void
startDialog(View v) {
Intent intent =
new
Intent(ActivityA.
this
, DialogActivity.
class
);
startActivity(intent);
}
public
void
startActivityB(View v) {
Intent intent =
new
Intent(ActivityA.
this
, ActivityB.
class
);
startActivity(intent);
}
public
void
finishActivityA(View v) {
ActivityA.
this
.finish();
}
}
Action
|
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
|
public
class
DialogActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_dialog);
System.out.println(
"DialogActivity-->onStart()"
);
}
@Override
protected
void
onStart() {
super
.onStart();
System.out.println(
"DialogActivity-->onStart()"
);
}
@Override
protected
void
onRestart() {
super
.onRestart();
System.out.println(
"DialogActivity-->onRestart()"
);
}
@Override
protected
void
onResume() {
super
.onResume();
System.out.println(
"DialogActivity-->onResume()"
);
}
@Override
protected
void
onPause() {
super
.onPause();
System.out.println(
"DialogActivity-->onPause()"
);
}
@Override
protected
void
onStop() {
super
.onStop();
System.out.println(
"DialogActivity-->onStop()"
);
}
@Override
protected
void
onDestroy() {
super
.onDestroy();
System.out.println(
"DialogActivity-->onDestroy()"
);
}
public
void
finishDialog(View v) {
DialogActivity.
this
.finish();
}
}
|