05 Activity 回传数据

来源:互联网 发布:淘宝商家的客服电话 编辑:程序博客网 时间:2024/06/06 02:25

当从一个Activity跳转到第二个Activity后然 让其处理完业务逻辑回传数据给第一个Activity:

回传调用方法顺序:

 onActivityResult---》》onRestart》》 start

第一个Activity:

package com.fmyboke;import java.io.Serializable;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.R.integer;import android.R.string;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.SimpleAdapter;import android.widget.Spinner;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {final int CODE =1;private Intent intent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.e("fmytext", "oncreate");intent = new Intent(this, Second.class);}@Overrideprotected void onStart(){super.onStart();Log.e("fmytext", "onStart");}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();Log.e("fmytext", "onResume");//开始跳转至 intent 意图   请求码//startActivityForResult(intent, CODE);}@Overrideprotected void onRestart() {// TODO Auto-generated method stubsuper.onRestart();Log.e("fmytext", "onRestart");}public void  click(View v) {Toast.makeText(this, "dianji", 0).show();startActivityForResult(intent, CODE);}//当回传数据的时候调用 会先用//当回传数据的时候调用 会先用调用用这个回传方法才    onActivityResult---》》onRestart》》 start@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {//TODO Auto-generated method stubLog.e("fmytext", "onActivityResult");super.onActivityResult(requestCode, resultCode, data);String name = data.getStringExtra("name");Toast.makeText(this, name, 0).show();TextView tv =(TextView) findViewById(R.id.tv2);tv.setText(name+":");}}
第二个回传:
package com.fmyboke;import java.io.Serializable;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class Second extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.second);Intent intent = new Intent();intent.putExtra("name", "张三");//设置响应码 和意图setResult(100, intent);//摧毁一个Activity}public void s(View v) {finish();}}



0 0
原创粉丝点击