启停活动页面
Activity 启动与结束

效果图:

第一个页面
点击之后出现

代码演示:
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 的生命周期


通过添加标记和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 和 运行函数的顺序标记
刚打开应用(软件运行)
有以下回显:

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

点击”返回“出现

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

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

Activity 启动模式


四种启动模式






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

效果图:
打开软件之后出现

点击按钮后

实现的就是两个页面相互跳转
以下是代码实现:
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.登录成功后不再返回登录页面

效果图界面:
这是进入界面

点击按钮之后跳到

代码如下:
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); } }
|