安卓Activity跳转
分为三步:
- 创建Intent对象
- 设置从哪跳转到哪setClass
- 跳转startActivity
Intent intent = new Intent();
intent.setClass(MainActivity.this, // context
NewActivity.class); // class
//跳转到新的Activity
startActivity(intent);//不传递数据,并且不需要返回响应
跳转时携带数据
有三种携带方法
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即可)
以上的方法都是不需要返回一个对象的,如果想接收一个对方发回来的对象:
三步:
// 需要用这个方法,第二个参数是一个请求码 => 自定义
startActivityForResult(intent,LOGIN_REQUEST);
// 设置响应,在跳转到的Activity中写
setResult(200,response);
// 做出反应
// 。。。。。。