zoukankan      html  css  js  c++  java
  • 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();
    	}
    }
    



  • 相关阅读:
    Python 标准库 BaseHTTPServer 中文翻译
    DBA Scripts
    cordova开发自己定义插件
    ASUS主板ALC887声卡,RTL81XX网卡,黑苹果驱动安装
    com.apple.installer.pagecontroller 错误 -1 pkg安装错误
    Apple 远程推送APNS 服务
    bitbucket git push 项目503失败
    在xcode5下利用Source Control 做 git 项目管理
    iOS设备定位服务开启判定
    has been modified since the precompiled header地图错误
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152298.html
Copyright © 2011-2022 走看看