安卓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();