赞
踩
目录
从当前页面跳到新页面,跳转代码如下:
startActivity(new Intent(源页面.this,目标页面.class));
从当前页面回到上一页面,相当于关闭当前页面,返回代码如下:
finish();//结束当前的活动页面
两个activity,点击第一个activity的跳转按钮即可跳转到第二个activity,点击返回或完成即可回到第一个activity。
第一个activity,java类
- 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.act_next).setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- startActivity(new Intent(this,ActFinishActivity.class));
-
- }
- }
xml文件
- <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"
- android:gravity="center">
-
- <Button
- android:id="@+id/act_next"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="跳转"/>
-
- </LinearLayout>
第二个activity,Java类
- 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.iv_back).setOnClickListener(this);
- findViewById(R.id.btn_finish).setOnClickListener(this);
- }
-
- @Override
- public void onClick(View v) {
- if(v.getId() == R.id.iv_back || v.getId() == R.id.btn_finish){
- finish();//结束当前页面
- }
- }
- }
xml文件
- <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="40dp"
- android:layout_height="40dp"
- 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:gravity="center"
- android:text="完成"/>
-
- </LinearLayout>
创建活动,把页面布局加载进内存,进入了初始状态。
开始活动,把活动页面显示在屏幕上,进入就绪状态。
恢复活动,活动页面进入活跃状态,能够与用户正常交互,例如允许相应用户的点击动作、允许用户输入文字等。
暂停活动,页面进入暂停状态,无法与用户正常交互。
停止活动,页面将不在屏幕显示。
销毁活动,回收活动占用的系统资源,把页面从内存中清除。
重启活动,重新加载内存中的页面数据。
重用已有的活动实例。
打开新页面的方法调用顺序为:
关闭旧页面的方法调用顺序为:
某个APP先后打开两个活动,此时活动栈的变动情况如下:
依次结束已打开的两个活动,此时活动栈的变动情况如下:
不在manifest设定时,Activity的默认模式是standard,该模式下启动的Activity会依照启动顺序被依次压入Task栈中。
该模式下,如果栈顶Activity为我们要新建的Activity(目标Activity),那么就不会重复创建新的Activity。
应用场景:适合开启渠道多、多应用开启调用的Activity,通过这种设置可以避免已经创建过的Activity被重复创建,多数通过动态设置使用。如:微信、支付宝等。
与上一模式相似,只不过singleTop模式是只针对栈顶的元素,而singleTask模式下,如果task栈内存在目标Activity实例,则将task内的对应Activity实例之上的所有Activity弹出栈,并将对应Activity置于栈顶,获得焦点。
例:下图,activity1跳转到2再跳转到3,再跳转到1时,将上面两个activity弹出,回到1。
应用场景:
该模式下,会为目标Activity创建一个新的Task栈,将目标Activity放入新的Task,并让目标Activity获得焦点,新的Task有且只有这一个Activity实例。如果已经创建过目标Activity实例,则不会创建新的Task,而是将以前创建过的Activity唤醒。
启动模式可以在配置文件中配置,也可以在java代码中配置。
接下来举两个例子阐述启动模式的实际应用:在两个活动之间交替跳转、登录成功后不再返回登录界面。
跳转流程:首页 -> 活动A ->活动B -> 活动A ->活动B -> 活动A ->活动B......
然后再返回,正常的流程是这样的:.... -> 活动B -> 活动A-> 活动B -> 活动A.....
要实现只返回一次页面:活动B -> 活动A -> 首页
可以设置启动标志FLAG_ACTIVITY_CLEAR_TOP,即使活动栈里存在待跳转的活动实例,也会重新创建该活动的实例,并清除原实例上方的所有实例,保证栈中最多只有该活动的唯一实例,从而避免的重复返回。
第一个页面java类
- public class activity_jump_first 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 view) {
- //创建意图对象,准备跳到指定活动页面
- Intent intent = new Intent(this,activity_jump_second.class);
- //栈中存在待跳转的活动实例时,则重新创建该活动的实例,并清除原实例上方的所有实例
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- }
-
- }
xml文件
- <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">
-
- <Button
- android:id="@+id/btn_jump_second"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="跳到第二个页面"/>
-
- </LinearLayout>
第二个java类
- public class activity_jump_second 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_second).setOnClickListener(this);
-
- }
-
- @Override
- public void onClick(View view) {
- //创建意图对象,准备跳到指定活动页面
- Intent intent = new Intent(this,activity_jump_first.class);
- //栈中存在待跳转的活动实例时,则重新创建该活动的实例,并清除原实例上方的所有实例
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- }
- }
xml文件
- <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">
-
- <Button
- android:id="@+id/btn_jump_second"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="跳到第二个页面"/>
-
- </LinearLayout>
可以设置启动标志FLAG_ACTIVITY_CLEAR_TASK,该标志会清空当前活动栈里的所有实例,全部清空后,当前栈没法继续使用,必须找另外个活动栈,也就是同时设置启动标志FLAG_ACTIVITY_NEW_TASK,该标志用于开辟新的活动栈。
第一个java类
- public class activity_jump_first 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_success).setOnClickListener(this);
- }
-
- @Override
- public void onClick(View view) {
- //创建意图对象,准备跳到指定活动页面
- Intent intent = new Intent(this,activity_jump_second.class);
- //设置启动标志,跳转到新页面时,栈中的原有实例都被清空,同时开辟新活动栈
- // 运算符|:参加运算的两个对象只要有一个为1,其值为1.
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);
- }
- }
xml文件
- <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: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>
第二个java类
- public class activity_jump_second extends AppCompatActivity{
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_jump_second);
- }
- }
xml文件
- <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:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="登录成功页面,不会再返回登录页面"/>
-
- </LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。