Android回调

当A页面跳往B页面做一些操作后,再从B页面回到A页面时,A页面想要回去一些B页面操作的数据时,我们一般会使用回调。

 1 public class MainActivity extends Activity {
 2
 3     TextView tv_text1;
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_main);
 8         tv_text1 = (TextView)findViewById(R.id.tv_text1);
 9     }
10
11     public void onclick(View v){
12         startActivityForResult(new Intent(MainActivity.this,Main2Activity.class),1);
13     }
14
15     @Override
16     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
17         if(requestCode==1&&resultCode==2){
18
19             String str = data.getStringExtra("key");
20             tv_text1.setText(str);
21         }
22     }
23 }
 1 public class Main2Activity extends AppCompatActivity {
 2
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main2);
 7     }
 8
 9     public void click1(View v){
10
11         Intent intent = new Intent();
12         intent.putExtra("key","this is result");
13         Main2Activity.this.setResult(2,intent);
14         Main2Activity.this.finish();
15     }
16 }

较为简单,所以直接上代码了。

上一篇:C# 接口使用方法


下一篇:List、Set、Map的区别