1. 两个Activity之间普通跳转
从一个Activity跳至另一个Activity,需要用到Intent(意图)这个东东,代码示例如下:
Intent intent = new Intent(当前Activity.this,目标Activity.class);
当前Activity.this.startActivity(intent);
2. 从源Activity跳转至目标Activity,目标Activity再回传值给源Activity
在Activity跳转中,如果需要Activity的回传值,再进行其他操作,怎么实现呢?
1). 源Activity重写onResume()方法
这种方式一般情况下是不推荐,但是针对一些特殊情况推荐使用,毕竟android中Activity跳转时,一般接收的是值类型的信息,这个方法可以更全面的接收回传的信息。这个就不做解释了,做项目时用过,还比较好用,就是代码写的时候会相对复杂些。
2). 源Activity重新onActivityResult()方法
Demo代码如下:
源Activity:
[html] view plaincopy
-
import
android.app.Activity; -
import
android.content.Intent; -
import
android.os.Bundle; -
import
android.view.View; -
import
android.view.View.OnClickListener; -
import
android.widget.Button; -
import
android.widget.TextView; -
-
public
class MainActivity extends Activity { -
-
//定义显示接收Activity传入值的TextView -
private TextView tv_main_result = null; -
//定义跳转的Button按钮 -
private Button btn_main_skip = null; -
//定义一个startActivityForResult()方法用到的整型值 -
private final int requestCode = 1500; -
@Override -
protected void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.activity_main); -
initView(); -
} -
-
public void initView(){ -
tv_main_result = (TextView)findViewById(R.id.tv_main_result); -
btn_main_skip = (Button)findViewById(R.id.btn_main_skip); -
btn_main_skip.setOnClickListener(new OnClickListener() { -
-
@Override -
public void onClick(View v) { -
Intent intent = newIntent(MainActivity.this,RsActivity.class); -
//采用Intent普通传值的方式 -
intent.putExtra("skip", "我是MainActivity传过来的值!"); -
//跳转Activity -
startActivityForResult(intent, requestCode); -
} -
}); -
} -
-
-
-
@Override -
protected void onActivityResult(int requestCode, int resultCode, Intent data) { -
super.onActivityResult(requestCode, resultCode, data); -
switch(resultCode){ -
case RESULT_OK:{//接收并显示Activity传过来的值 -
Bundle bundle = data.getExtras(); -
String rs = bundle.getString("rs"); -
tv_main_result.setText(rs); -
break; -
} -
default: -
break; -
} -
} - }
目标Activity:
[java] view plaincopy
-
import
android.app.Activity; -
import
android.content.Intent; -
import
android.os.Bundle; -
import
android.view.View; -
import
android.view.View.OnClickListener; -
import
android.widget.Button; -
import
android.widget.TextView; -
-
public
class RsActivity extendsActivity { -
-
//定义显示接收Activity传入值的TextView -
private TextView null;tv_rs_result = -
//定义跳转的Button按钮 -
private Button null;btn_rs_skip = -
-
@Override -
protected void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.activity_rs); -
initView(); -
} -
-
-
private void initView(){ -
//接收Activity传过来的值 -
final Intent data = getIntent(); -
tv_rs_result = (TextView)findViewById(R.id.tv_rs_result); -
//将Activity传过来的值赋给textview显示 -
tv_rs_result.setText(data.getStringExtra("skip")); -
-
btn_rs_skip = (Button)findViewById(R.id.btn_rs_skip); -
btn_rs_skip.setOnClickListener(new OnClickListener() { -
-
@Override -
public void onClick(View //采用Intent绑定Bundle的形式回传值v) { -
-
//新建一个Bundle,Bundle主要放值类型 -
Bundle bundle = new Bundle(); -
bundle.putString("rs", "我是RsActivity关闭后回传的值!"); -
//将Bundle赋给Intent -
data.putExtras(bundle); -
//跳转回MainActivity -
//注意下面的RESULT_OK常量要与回传接收的Activity中onActivityResult()方法一致 -
RsActivity.this.setResult(RESULT_OK, data); -
//关闭当前activity -
RsActivity.this.finish(); -
} -
}); -
} - }