第一个例子:demo1
Main—>SecondActivity—>Main
从流程上看就是从Main跳转到SecondActivity,再从SecondActivity返回到Main。也就是将子Activity的数据返回给父Activity。
代码如下:
- public class Main extends Activity {
- private Button btn;
- private TextView request_text;
- private final int FIRST_REQUEST_CODE = 1;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- request_text = (TextView)this.findViewById(R.id.request_text);
- btn = (Button)this.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent(Main.this,SecondActivity.class);
- i.putExtra("request_text_for_main", "从Main传递到SecondActivity");
- startActivityForResult(i, FIRST_REQUEST_CODE);
- }
- });
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_OK){
- if(data != null) {
- request_text.setText(data.getStringExtra("request"));
- }
- }
- }
- }
Java代码
- public class SecondActivity extends Activity{
- private TextView request_text;
- private Button btn;
- private String text;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
- request_text = (TextView)this.findViewById(R.id.request_text);
- if (getIntent().getStringExtra("request_text_for_main") != null) {
- text = getIntent().getStringExtra("request_text_for_main");
- request_text.setText(text);
- }
- btn = (Button)this.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent();
- i.putExtra("request",text+" "+"从SecondActivity传递到ThirdActivity");
- setResult(Activity.RESULT_OK,i);
- finish();
- }
- });
- }
- }
图一
图一所示是demo初始化的样子,点击“第一次跳转”进入图二。
图二
图二显示从Main到SecondActivity这个过程,再点击“第二次跳转”即返回Main这个Activity。
图三
图三表示跳转结束,Button按钮上面的文字记录跳转的过程。
这个例子最主要是第一个Activity Main设置的onActivityResult()方法和startActivityForResult()方法。
在Main中通过startActivityForResult()调用SecondActivity,然后在onActivityResult()中获取其返回参数,或者说通过onActivityResult()进行回调。
SecondActivity中要返回第一个Main必须:
setResult(Activity.RESULT_OK,i);
finish();
第二个例子:demo2
Main—>SecondActivity—>ThirdActivity—>Main
这个例子的流程,比上一个更复杂一些,经历了3个Activity,最好跳转回第一个Activity。
代码如下:
Java代码
- public class Main extends Activity {
- private Button btn;
- private TextView request_text;
- private final int FIRST_REQUEST_CODE = 1;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- request_text = (TextView)this.findViewById(R.id.request_text);
- btn = (Button)this.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent(Main.this,SecondActivity.class);
- i.putExtra("request_text_for_main", "从Main传递到SecondActivity");
- startActivityForResult(i, FIRST_REQUEST_CODE);
- }
- });
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_FIRST_USER){
- if(data != null) {
- request_text.setText(data.getStringExtra("request_text_for_third"));
- }
- }
- }
- }
Java代码
- public class SecondActivity extends Activity{
- private TextView request_text;
- private Button btn;
- private final int SECOND_REQUEST_CODE = 2;
- private String text;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
- request_text = (TextView)this.findViewById(R.id.request_text);
- if (getIntent().getStringExtra("request_text_for_main") != null) {
- text = getIntent().getStringExtra("request_text_for_main");
- request_text.setText(text);
- }
- btn = (Button)this.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent(SecondActivity.this,ThirdActivity.class);
- i.putExtra("request_text_for_second", text+" "+"从SecondActivity传递到ThirdActivity");
- startActivityForResult(i, SECOND_REQUEST_CODE);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode==SECOND_REQUEST_CODE && data != null){
- setResult(Activity.RESULT_FIRST_USER, data);
- finish();
- }
- }
- }
Java代码
- public class ThirdActivity extends Activity{
- private TextView request_text;
- private Button btn;
- private String text;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.third);
- text = getIntent().getStringExtra("request_text_for_second");
- request_text = (TextView)this.findViewById(R.id.request_text);
- request_text.setText(text);
- btn = (Button)this.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent();
- i.putExtra("request_text_for_third", text+" "+"从ThirdActivity再次传递到Main");
- setResult(Activity.RESULT_FIRST_USER, i);
- finish();
- }
- });
- }
- }
这个demo2比起前面的demo1的不同之处在于就是通过ThirdActivity进行跳转到Main。
在代码上的区别是:
(1)Main的onActivityResult()方法中,它的resultCode==Activity.RESULT_FIRST_USER而不是前面的resultCode==Activity.RESULT_OK
(2)SecondActivity中增加了onActivityResult()方法,该方法是用于回调ThirdActivity,并且该方法执行了跳转过程跳转到Main这个Activity。
(3) ThirdActivity和demo1的SecondActivity很类似。
第三个例子:demo3
Main—>SecondActivity—>ThirdActivity—>SecondActivity—>Main
这个过程看起来复杂,其实不然,最后一步是借助SecondActivity的另一个Button实现的。
代码如下:
Java代码
- public class Main extends Activity {
- private Button btn;
- private TextView request_text;
- private final int FIRST_REQUEST_CODE = 1;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- request_text = (TextView)this.findViewById(R.id.request_text);
- btn = (Button)this.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent(Main.this,SecondActivity.class);
- i.putExtra("request_text_for_main", "从Main传递到SecondActivity");
- startActivityForResult(i, FIRST_REQUEST_CODE);
- }
- });
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_FIRST_USER){
- if(data != null) {
- request_text.setText(data.getStringExtra("final_request"));
- }
- }
- }
- }
Java代码
- public class SecondActivity extends Activity{
- private TextView request_text;
- private Button btn,finalBtn;
- private final int SECOND_REQUEST_CODE = 2;
- private String text;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
- request_text = (TextView)this.findViewById(R.id.request_text);
- if (getIntent().getStringExtra("request_text_for_main") != null) {
- text = getIntent().getStringExtra("request_text_for_main");
- request_text.setText(text);
- }
- btn = (Button)this.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent(SecondActivity.this,ThirdActivity.class);
- i.putExtra("request_text_for_second", text+" "+"从SecondActivity传递到ThirdActivity");
- startActivityForResult(i, SECOND_REQUEST_CODE);
- }
- });
- finalBtn = (Button)this.findViewById(R.id.finalBtn);
- finalBtn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent();
- i.putExtra("final_request", text+" "+"从SecondActivity最后传递到Main");
- setResult(Activity.RESULT_FIRST_USER,i);
- finish();
- }
- });
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode==SECOND_REQUEST_CODE && resultCode==Activity.RESULT_OK){
- if(data != null) {
- text = data.getStringExtra("request_text_for_third");
- request_text.setText(text);
- }
- }
- }
- }
Java代码
- public class ThirdActivity extends Activity{
- private TextView request_text;
- private Button btn;
- private String text;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.third);
- text = getIntent().getStringExtra("request_text_for_second");
- request_text = (TextView)this.findViewById(R.id.request_text);
- request_text.setText(text);
- btn = (Button)this.findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent i = new Intent();
- i.putExtra("request_text_for_third", text+" "+"从ThirdActivity再次传递到SecondActivity");
- setResult(Activity.RESULT_OK, i);
- finish();
- }
- });
- }
- }
比起前一个demo2其实没有更复杂之处,只是从ThirdActivity跳转回SecondActivity,而通过SecondActivity的另一个Button来跳转回Main的。