Androidactivity跳转代码
时间: 2023-11-03 22:29:51 浏览: 100
Android中Activity之间的跳转可以通过Intent来实现,具体的代码如下所示:
在当前Activity中跳转到目标Activity:
```java
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
startActivity(intent);
```
如果需要传递参数,则可以通过Intent的putExtra方法来实现:
```java
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.putExtra("key", value);
startActivity(intent);
```
在目标Activity中获取参数的值:
```java
Intent intent = getIntent();
String value = intent.getStringExtra("key");
```
如果需要在目标Activity中返回结果,则可以通过setResult方法设置返回结果,并在目标Activity中调用finish方法关闭当前Activity:
在当前Activity中启动目标Activity:
```java
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
startActivityForResult(intent, requestCode);
```
在目标Activity中设置返回结果并关闭当前Activity:
```java
Intent intent = new Intent();
intent.putExtra("key", value);
setResult(resultCode, intent);
finish();
```
在当前Activity中获取返回结果:
```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == requestCode && resultCode == RESULT_OK) {
String result = data.getStringExtra("key");
}
}
```
阅读全文