一个Android通常由多个Activity组成,其中一个Activity是作为应用程序的入口。Activity启动其他的Activity通常有一下几种方法:
1.两个Activity最简单的跳转
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 v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
2. 使用Bundle在两个Activity之间交换数据
两个Activity之间,用Intent携带数据,只需要将要携带的数据放入Intent即可。
Intent提供了多种方法来携带额外的数据,主要有以下:
- putExtras(Bundle b): 向Intent中放入需要携带的数据包
- Bundle getExtras():取出Intent所携带的数据包
- putExtra(String key, XXX value) :向Intent 中按key - value 对的形式存入数据(本质上还是利用的Bundle来进行的数据存储)
- getXXXExtra(String key): 从Intent中按key取出指定类型的数据
举例:FirstActivity用于收集用户数据,提交到SecondActivity进行展示。
- FirstActivity
public class FirstActivity extends AppCompatActivity {
private EditText etUserName;
private EditText etPassword;
private RadioGroup rgGender;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUserName = findViewById(R.id.et_user_name);
etPassword = findViewById(R.id.et_password);
rgGender = findViewById(R.id.radioGroup);
btnSubmit = findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String gender = "女";
if (rgGender.getCheckedRadioButtonId() == R.id.rb_male) {
gender = "男";
}
Person person = new Person(etUserName.getText().toString(), etPassword.getText().toString(), gender);
Bundle b = new Bundle();
b.putSerializable("person", person);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(b);
startActivity(intent);
}
});
}
}
- SecondActivity:
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras = getIntent().getExtras();
((TextView) findViewById(R.id.textView)).setText(((Person) extras.getSerializable("person")).toString());
}
}
3. 启动Activity并返回结果
startActivityForResult(Intent intent, int resultCode)
这个方法用于启动指定的Activity并请求获取指定Activity的返回结果。
- 当前Activity需要重写onActivityResult(int requestCode, int resultCode, Intent intent),当被启动的Activity 返回结果时,该方法将会触发,requestCode代表请求码,resultCode代表Activity的返回码。
- 被启动的Activity需要调用setResult()方法设置处理结果。
举例:
第一个Activity是一个简单的布局,包含一个按钮和文本显示框
点击按钮跳转到第二个Activity选择城市,返回结果显示在第一个Activity中。
第一个Acitivity:
public class MainActivity extends AppCompatActivity {
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
}
public void chooseCity(View view) {
Intent intent = new Intent(MainActivity.this, SelectCityActivity.class);
//1001是请求码
startActivityForResult(intent, 1001);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001 && resultCode == 1002) {
Bundle bundle = data.getExtras();
if (bundle != null) {
result.setText(bundle.getString("city"));
}
}
}
}
第二个Activity
public class SelectCityActivity extends AppCompatActivity {
...
private static final String[] CITIES = {"北京", "上海", "广州"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.item_view, CITIES);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Intent intent = getIntent();
intent.putExtra("city", CITIES[position]);
SelectCityActivity.this.setResult(1002, intent); //结果码
finish();
}
});
}
}
tobecontinued