启停活动页面

Activity 启动与结束

720d7925807032b6.png

效果图:

290d11f6b0727673.png

第一个页面

点击之后出现

fea1f7e58763f781.png

代码演示:

activity_act_start.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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"
android:gravity="center">

<Button
android:id="@+id/btn_act_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到下一个页面"/>

</LinearLayout>

ActStartActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.luowen.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class ActStartActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_start);
findViewById(R.id.btn_act_next).setOnClickListener(this);
}

@Override
public void onClick(View v) {
startActivity(new Intent(this,ActFinishActivity.class));
}
}

activity_act_finish.xml

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/ic_back"/>

<Button
android:id="@+id/btn_finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="完成"
android:gravity="center"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按返回键,或者点击左上角的箭头图标,或者点击上面的完成按钮,均可关闭当前页面,返回上个页面"/>

</LinearLayout>

ActFinishActivity.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
package com.luowen.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class ActFinishActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_finish);
findViewById(R.id.btn_finish).setOnClickListener(this);
findViewById(R.id.iv_back).setOnClickListener(this);
}

@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_finish || v.getId() == R.id.iv_back){
//结束当前的活动界面
finish();
}
}
}

Activity 的生命周期

32d713ca44fd5937.png

9777e393ab1470d2.png

通过添加标记和log 来看生命周期的运行顺序

部分重要代码如下:

ActStartActivity.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
package com.luowen.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class ActStartActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG="ning";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"ActFinishActivity onCreate");
setContentView(R.layout.activity_act_start);
findViewById(R.id.btn_act_next).setOnClickListener(this);
}

@Override
public void onClick(View v) {
startActivity(new Intent(this,ActFinishActivity.class));
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"ActFinishActivity onStart");
}

@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"ActFinishActivity onResume");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"ActFinishActivity onPause");
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"ActFinishActivity onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"ActFinishActivity onDestroy");
}

@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG,"ActFinishActivity onRestart");
}
}

其实程序还是上面启动与结束章节的就加了点 TAG 和 运行函数的顺序标记

刚打开应用(软件运行)

有以下回显:

7f94a97fcff59a36.png

点击“跳转到下一个界面”出现

31d44b14320d4ece.png

点击”返回“出现

eb6b86f21416d16d.png

退出应用后(包括划掉后台)

f0a0ea9716e25715.png

通过这些LOG回显来判断生命周期顺序

1cc7e6bf16e2c175.png

Activity 启动模式

5767d0127145a98a.png

04ae14f70e3b28b8.png

四种启动模式

de8b6bfa3aa7926c.png

1b296ead8a1c8718.png

68e78e68c273b4a8.png

98d760ae135ba54a.png

d2b2629fad150a37.png

c236593efd433344.png

1.在两个活动之间交替跳转

94d6ebcd2246babb.png

效果图:

打开软件之后出现

b16413937d998aea.png

点击按钮后

8bda1d7b64f69798.png

实现的就是两个页面相互跳转

以下是代码实现:

activity_jump_first.xml

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">

<Button
android:id="@+id/btn_jump_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到第二个页面"/>

</LinearLayout>

activity_jump_second.xml

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">

<Button
android:id="@+id/btn_jump_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到第一个页面"/>

</LinearLayout>

JumpFirstActivity.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
package com.luowen.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class JumpFirstActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jump_first);
findViewById(R.id.btn_jump_second).setOnClickListener(this);
}

@Override
public void onClick(View v) {
//创建一个意图对象,准备跳到指定的活动页面
Intent intent = new Intent(this, JumpSecondActivity.class);
//栈中存在待跳转的活动实例时,则重新创建该活动实例,并清楚原实例上方的所有实例
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

}
}

JumpSecondActivity.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
package com.luowen.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class JumpSecondActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jump_second);
findViewById(R.id.btn_jump_first).setOnClickListener(this);
}

@Override
public void onClick(View v) {
//创建一个意图对象,准备跳到指定的活动页面
Intent intent = new Intent(this, JumpFirstActivity.class);
//栈中存在待跳转的活动实例时,则重新创建该活动实例,并清楚原实例上方的所有实例
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}

2.登录成功后不再返回登录页面

75bd23c4f490b2a6.png

效果图界面:

这是进入界面

0be5f68e51f32dd9.png

点击按钮之后跳到

ff052f540ce90a3f.png

代码如下:

activity_login_input.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是登录验证页面,此处省略了用户名和密码等输入框"/>

<Button
android:id="@+id/btn_jump_success"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="跳到登录成功页面"/>
</LinearLayout>

activity_login_success.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是登录成功页面,登录成功之后不必返回登录验证页面。请按返回键看看"/>


</LinearLayout>

LoginInputActivity.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
package com.luowen.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class LoginInputActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_input);
findViewById(R.id.btn_jump_success).setOnClickListener(this);
}

@Override
public void onClick(View v) {
//创建一个意图对象,准备跳到指定的活动页面
Intent intent = new Intent(this, LoginSuccessActivity.class);
//设置启动标志:跳转到新页面时,栈中的原有实例都被清空,同时开辟新任务的活动栈
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}

LoginSuccessActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.luowen.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class LoginSuccessActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_success);
}
}