关闭

返回Activity时传递数据(如发送短信时引用联系人)

142人阅读 评论(0) 收藏 举报
分类:

从A界面打开B界面, B界面关闭的时候,返回一个数据给A界面

步骤: 1. 开启activity并且获取返回值

    startActivityForResult(intent, 0);
  1. 在新开启的界面里面实现设置数据的逻辑

    Intent data = new Intent();
    data.putExtra("phone", phone);
    //设置一个结果数据,数据会返回给调用者
    setResult(0, data);
    finish();//关闭掉当前的activity,才会返回数据
    
  2. 在开启者activity里面实现方法 onActivityResult(int requestCode, int resultCode, Intent data) 通过data获取返回的数据

  3. 根据请求码和结果码确定业务逻辑

MainActivity.java
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	}

	public void click(View c){
		//跳转至选择联系人Activity
		Intent intent = new Intent(this, ContactActivity.class);
//		startActivity(intent);
		//用这个api启动的Activity,在销毁时,系统会回调onActivityResult
		startActivityForResult(intent, 10);
	}
	
	
	public void click2(View v){
		//跳转至选择快捷回复的Activity
		Intent intent = new Intent(this, CallbackActivity.class);
		startActivityForResult(intent, 20);
	}
	//如果有Activity在销毁时返回了数据,那么就会调用此方法来接收数据
	//requestCode:用来区分数据来自于哪一个Activity
	//resultCode:用来区分返回的数据是什么类型的
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		String name = data.getStringExtra("name");
		if(requestCode == 10){
			EditText et = (EditText)findViewById(R.id.et);
			et.setText(name);
		}
		else if(requestCode == 20){
			EditText et_content = (EditText)findViewById(R.id.et_content);
			et_content.setText(name);
		}
	}
	

}
ContactActivity.java
package com.itheima.getresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ContactActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		
		ListView lv = (ListView) findViewById(R.id.lv);
		
		final String[] objects = new String[]{
				"小志",
				"逼哥",
				"世界级XXX",
				"国服第一"
		};
		
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv, objects));
	
		//给listview设置条目的点击侦听
		lv.setOnItemClickListener(new OnItemClickListener() {

			//当某个条目被点击时,此方法调用
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				//Activity返回时传递数据,也是通过意图对象
				Intent data = new Intent();
				//把要传递的数据封装至意图对象中
				data.putExtra("name", objects[position]);
				
				//当前Activity销毁时,data这个意图就会传递给启动当前Activity的那个Activity
				setResult(1, data);
				
				//销毁当前Activity
				finish();
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}
}
CallBackActivity.java

package com.itheima.getresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CallbackActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact);
		
		ListView lv = (ListView) findViewById(R.id.lv);
		
		final String[] objects = new String[]{
				"免谈,没戏,滚犊子",
				"媳妇我错了,求原谅",
				"老子才是一家之主"
		};
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv, objects));
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent data = new Intent();
				data.putExtra("name", objects[position]);
				
				setResult(2, data);
				finish();
				
			}
		});
	}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:102158次
    • 积分:2669
    • 等级:
    • 排名:第14537名
    • 原创:147篇
    • 转载:143篇
    • 译文:1篇
    • 评论:4条
    最新评论