目录
Activity
activity-屏幕组件
启动停止
从当前页面跳转到新页面:
startActivity(new Intent(原页面.this,,目标页面.class))
关闭当前页面,从当前页面返回上一个页面:
finish()
生命周期
启动模式
APP启动后,activity进入任务栈(先进后出),按返回键可以回到上一级activity
Java动态设置启动模式:intent.setFlags()
意图-Intent
活动之间通信的桥梁
组成部分
显式Intent
直接指定来源活动与目标活动
构建方法:
Intent intent1 = new Intent(MainActivity.this, activity_first.class);
//创建意图对象 Intent intent = new Intent(); //设置意图要跳转的目标活动 intent.setClass(MainActivity.this,activity_first.class);
//创建意图对象 Intent intent = new Intent(); //创建包含目标活动在内的组件名称 对象 ComponentName componentName = new ComponentName(MainActivity.this, activity_first.class); //设置意图携带的组件信息 intent.setComponent(componentName);
隐式Intent
没有明确指定目标活动,只给出一个动作字符串,让系统模糊匹配
Intent intent = new Intent(); String phone = "12345"; //隐式跳转,系统拨号,准备拨号 intent.setAction(Intent.ACTION_DIAL); Uri uri = Uri.parse("tel:"+phone); //发送短信 intent.setAction(Intent.ACTION_SENDTO); //隐式跳转,发短信 Uri uri = Uri.parse("smsto:"+phone) //传送数据,跳转 intent.setData(uri); startActivity(intent);
跳转到自定义的动作:
1.确定目标活动的启动方式
2. 当前活动跳转设置:
intent.setAction("android.intent.action.DENG"); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivity(intent);
数据性传递
发送数据
bundle对传输数据进行打包
Intent intent = new Intent(this,MainActivity.class); Date date = new Date(); Bundle bundle = new Bundle(); //要发送的数据打包 bundle.putString("time", String.valueOf(date)); //数据 bundle.putString("content",tv_send.getText().toString()); intent.putExtras(bundle); startActivity(intent);
接收数据并显示
//接收其他页面传送的数据 TextView result = findViewById(R.id.result); //获取传送数据的包裹 Bundle bundle = getIntent().getExtras(); //获取数据 String time = bundle.getString("time"); String content = bundle.getString("content"); //格式化输出 String text = String.format("时间:%s 文本:%s",time,content); result.setText(text);
回复-返回数据
A->B,B接收后回复A
发送方A:
package com.example.myapplication;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Date;
public class activity_bundle extends AppCompatActivity implements View.OnClickListener {
private TextView tv_send;
private ActivityResultLauncher<Intent> register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bundle);
tv_send = findViewById(R.id.tv_send);
findViewById(R.id.btn_send).setOnClickListener(this);
//注册register,用于跳转
register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
//回调方法
@Override
public void onActivityResult(ActivityResult result) {
if (result != null){
Intent resultData = result.getData();
if (resultData != null && result.getResultCode() == Activity.RESULT_OK){
//如果处理成功才进行
Bundle bundle = resultData.getExtras();
String repose = bundle.getString("repose");
tv_send.setText(repose);
}
}
}
});
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this,MainActivity.class);
Date date = new Date();
Bundle bundle = new Bundle(); //要发送的数据打包
bundle.putString("time", String.valueOf(date)); //数据
bundle.putString("content",tv_send.getText().toString());
intent.putExtras(bundle);
register.launch(intent);
}
}
接收方B(接收A信息处理后回复):
//创建意图对象-实现上下文跳转
Intent intent = new Intent();
//返回,并携带数据
Bundle bundle1 = new Bundle();
bundle1.putString("repose","你好");
intent.putExtras(bundle1);
//携带意图返回上一页面,RESULT_OK处理成功
setResult(Activity.RESULT_OK,intent);
//结束该页面
finish();