- 延迟后自动跳转页面
使用Handler的postDelayed方法
handler.postDelayed(
new Runnable() {
@Override
public void run() {
UserInfo userInfo = new UserInfo();
userInfo.setName("小明");
userInfo.setSex("男");
//跳转到MainActivity
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
intent.putExtra("title",title);
intent.putExtra("userInfo",userInfo);
startActivity(intent);
}
},1000
);
- 传递基本类型数据
使用Intent的putExtra方法
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
intent.putExtra("title",title);
- 传递引用类型数据
实体类需要实现java.io.Serializable接口
import java.io.Serializable;
public class UserInfo implements Serializable {
private String name;
private String sex;
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public UserInfo() {
}
public UserInfo(String name, String sex) {
this.name = name;
this.sex = sex;
}
}
UserInfo userInfo = new UserInfo();
userInfo.setName("小明");
userInfo.setSex("男");
//跳转到MainActivity
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
intent.putExtra("userInfo",userInfo);
- 获取Intent传递过来的数据
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View view) {
/* Toast.makeText(MainActivity.this,"hello,hello",Toast.LENGTH_LONG).show();*/
Intent intent = new Intent(MainActivity.this,SplashActivity.class);
startActivity(intent);
}
}
);
Intent intent = getIntent();
if(intent!=null){
String title = intent.getStringExtra("title");
UserInfo info = (UserInfo) intent.getSerializableExtra("userInfo");
setTitle(title+" "+info.getName());
}
}
}