码迷,mamicode.com
首页 > 其他好文 > 详细

02-startActivityForResult及Intent数据传递

时间:2018-04-26 21:03:26      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:turn   break   space   ati   rar   with   result   www   res   

通过点击按钮,跳转到A02_Target,在A02_Target通过按钮或返回键返回,将数据传递回去
 
A02_ForResult
 1 package top.toly.unitcommon.activity.item2;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.widget.Button;
 6 import android.widget.TextView;
 7 import butterknife.Bind;
 8 import butterknife.ButterKnife;
 9 import butterknife.OnClick;
10 import top.toly.unitcommon.R;
11 import top.toly.www.zlibrary.bean.Person;
12 
13 public class A02_ForResult extends AppCompatActivity {
14 
15     private static final int DATA_CODE = 0x0001;
16 
17     @Bind(R.id.btn_for_result)
18     Button mBtnForResult;
19     @Bind(R.id.tv_result)
20     TextView mTvResult;
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.a02_for_result);
26         ButterKnife.bind(this);
27     }
28 
29     @OnClick(R.id.btn_for_result)
30     public void onViewClicked() {
31         startActivityForResult(new Intent(this, A02_Target.class), DATA_CODE);
32     }
33 
34     @Override
35     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
36 
37         switch (requestCode) {
38             case DATA_CODE:
39                 if (resultCode == RESULT_OK) {
40                     String dataFormTarget = data.getStringExtra("data");
41 
42                     Bundle personData = data.getBundleExtra("personData");
43                     Person person = (Person) personData.get("person");
44                     mTvResult.setText("dataFormTarget:" + dataFormTarget
45                             + "\nperson:" + person.toString());
46                 }
47                 break;
48         }
49     }
50 }
A02_Target
 1 package top.toly.unitcommon.activity.item2;
 2 
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.widget.Button;
 7 
 8 import butterknife.Bind;
 9 import butterknife.ButterKnife;
10 import butterknife.OnClick;
11 import top.toly.unitcommon.R;
12 import top.toly.www.zlibrary.bean.Person;
13 
14 public class A02_Target extends AppCompatActivity {
15 
16     @Bind(R.id.btn_send)
17     Button mBtnSend;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.a02_target);
23         ButterKnife.bind(this);
24 
25     }
26 
27     @OnClick(R.id.btn_send)
28     public void onViewClicked() {
29         backWithData();
30         finish();
31     }
32 
33     private void backWithData() {
34         Person jt = new Person("捷特", 24);
35 
36         Intent intent = new Intent();
37         intent.putExtra("data", "我是A02_Target的数据");
38 
39         Bundle bundle = new Bundle();
40         bundle.putSerializable("person", jt);
41         intent.putExtra("personData", bundle);
42         setResult(RESULT_OK, intent);
43     }
44 
45     /**
46      * 重写返回键
47      */
48     @Override
49     public void onBackPressed() {
50         backWithData();
51         super.onBackPressed();
52 
53     }
54 }

person 要用intent传递对象,可以让person实现Serializable

 1 package top.toly.www.zlibrary.bean;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * 作者:张风捷特烈
 7  * 时间:2018/4/26:12:13
 8  * 邮箱:1981462002@qq.com
 9  * 说明:
10  */
11 public class Person implements Serializable {
12 
13     private String name;
14     private int age;
15 
16     public Person(String name, int age) {
17         this.name = name;
18         this.age = age;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public int getAge() {
30         return age;
31     }
32 
33     public void setAge(int age) {
34         this.age = age;
35     }
36 
37     @Override
38     public String toString() {
39         return "Person{" +
40                 "name=‘" + name + ‘\‘‘ +
41                 ", age=" + age +
42                 ‘}‘;
43     }
44 }

 

02-startActivityForResult及Intent数据传递

标签:turn   break   space   ati   rar   with   result   www   res   

原文地址:https://www.cnblogs.com/toly/p/8954717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!