05 Activity 回传数据

当从一个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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("fmytext", "oncreate");
intent = new Intent(this, Second.class); } @Override
protected void onStart(){
super.onStart();
Log.e("fmytext", "onStart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("fmytext", "onResume"); //开始跳转至 intent 意图 请求码
//startActivityForResult(intent, CODE);
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e("fmytext", "onRestart");
}
public void click(View v) {
Toast.makeText(this, "dianji", 0).show();
startActivityForResult(intent, CODE);
}
//当回传数据的时候调用 会先用
//当回传数据的时候调用 会先用调用用这个回传方法才 onActivityResult---》》onRestart》》 start
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//TODO Auto-generated method stub
Log.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{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = new Intent();
intent.putExtra("name", "张三");
//设置响应码 和意图
setResult(100, intent);
//摧毁一个Activity }
public void s(View v) {
finish();
}
}
上一篇:Android Studio 更新同步Gradle错误解决方法


下一篇:Leetcode 137 Single Number II 仅出现一次的数字