安卓Activity跳转

安卓Activity跳转

分为三步:

  1. 创建Intent对象
  2. 设置从哪跳转到哪setClass
  3. 跳转startActivity
	Intent intent = new Intent();
	intent.setClass(MainActivity.this, // context
	                        NewActivity.class); // class
    //跳转到新的Activity
	startActivity(intent);//不传递数据,并且不需要返回响应

跳转时携带数据

有三种携带方法

  1. 直接putExtra以键值对的方式存储

  2. 都封装在Bundle对象中,将Bundle对象以键值对的方式存储

  3. 传递一个自定义的对象

1.putExtra的直接存储方式

类似于下面的方法,在取出时注意类型

	// 存储
	intent.putExtra("phone", "1234565");
	intent.putExtra("email", "123132ees");
	// 另一个Activity中接收,如果存的时int就是getIntExtra
	String phone = request.getStringExtra("phone");
	String email = request.getStringExtra("email");

2.封装成Bundle的存储方式

	// 存储
	Bundle bundle = new Bundle(); // 创建Bundle对象
	bundle.putString("phone","123231121212"); // 放入什么类型的就putXXX()
	bundle.putString("email","adadasda");
	intent.putExtra("bundle",bundle);
	// 另一个Activity中接收
	Bundle bundle = request.getBundleExtra("bundle");
	String phone = bundle.getString("phone");
	String email = bundle.getString("email");

3.传递一个自定义的对象

	// 存储
	Student student = new Student("张三","121312312","123121");
	intent.putExtra("stu",student);
	// 另一个Activity中接收
	Student stu = (Student) request.getSerializableExtra("stu");
	String phone = stu.getPhone();
	String email = stu.getEmail();

PS:当传递一个自定义的对象时,该类需要实现Serializable接口,上面的例子中Student类实现了Serializable接口。(Serializable接口中没有任何需要实现的方法,直接implements即可)


以上的方法都是不需要返回一个对象的,如果想接收一个对方发回来的对象:

三步:

  1. 跳转时使用带有请求码的方法

  2. 跳转到的页面设置响应

  3. 跳转前页面做出对应操作

	// 需要用这个方法,第二个参数是一个请求码 => 自定义
	startActivityForResult(intent,LOGIN_REQUEST);
	// 设置响应,在跳转到的Activity中写
	setResult(200,response);
	// 做出反应
	// 。。。。。。

做出响应的样例代码:

在这里插入图片描述

PS:在区分时用请求码和响应码作区分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值