• android 之得到新打开Activity 关闭后返回的数据


    现在有这样的功能需求。在一个Activity (A)中打开另一个Activity(B).在另一个B 中添写数据,当这个B关闭时,把在B中填写的

    数据返回给A,并写显示出。这样就有必要用今天的功能了。

    以下为是代码。也就是Activity (A)

    package com.hkrt.intent;
    
    import com.hkrt.domain.Person;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class IntentDemoActivity extends Activity {
        /** Called when the activity is first created. */
    	private static final String LOG="IntentDemoActivity";
        @Override
        public void onCreate(Bundle savedInstanceState) {
        	Log.i(LOG, "onCreate");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button button =(Button)this.findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
    			@Override
    			public void onClick(View v) {
    			 Intent intent = new Intent(IntentDemoActivity.this,OtherActivity.class);
    			 // 方法一
    //			 intent.putExtra("name","张三");
    			 //方法二
    			 Bundle bun = new Bundle();
    			 bun.putString("name","张三");
    			 bun.putInt("age", 18);
    			 Person per = new Person("张三",5896.20);
    			 bun.putSerializable("person", per);
    			 intent.putExtras(bun);
    //			 startActivity(intent);
    			 startActivityForResult(intent, 10); 
    			}
    		});
        }
        //接收新的Activiity 返回的结果
        /**
         * requestCode:10
         * resultCode:100
         * */
        @Override
        protected void onActivityResult(int requestCode,int resultCode,Intent data){
        	Toast.makeText(this, data.getExtras().getString("name")+"requestCode:"+requestCode+"resultCode"+resultCode, 1).show();
        	super.onActivityResult(requestCode, resultCode, data);
        }
    	@Override
    	protected void onStart() {
    		Log.i(LOG, "onStart");
    		super.onStart();
    	}
    	
    	@Override
    	protected void onResume() {
    		Log.i(LOG, "onResume");
    		super.onResume();
    	}
    	
    
    	@Override
    	protected void onPause() {
    		Log.i(LOG, "onPause");
    		super.onPause();
    	}
    
    	@Override
    	protected void onRestart() {
    		Log.i(LOG, "onRestart");
    		super.onRestart();
    	}
    
    
    	@Override
    	protected void onStop() {
    		Log.i(LOG, "onStop");
    		super.onStop();
    	}
    
    	@Override
    	protected void onDestroy() {
    		Log.i(LOG, "onDestroy");
    		super.onDestroy();
    	}
    }

    Activity(B).

    package com.hkrt.intent;
    
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.hkrt.domain.Person;
    
    public class OtherActivity extends Activity {
      private static final String LOG="OtherActivity";
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		Log.i(LOG, "onCreate");
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.other);
    		Intent intent = this.getIntent();
    		//方法一
    //		String name = intent.getStringExtra("name");
    		//方法二
    		Bundle  bun =  intent.getExtras();
    		String name1 = bun.getString("name");
    		int age = bun.getInt("age",10);
    		TextView textView = (TextView)this.findViewById(R.id.textView);
    		
    		Person per =	(Person)bun.getSerializable("person");
    		textView.setText("姓名:"+name1+"\t年龄:"+age+"\tperson:"+per.getName()+"\tmoney:"+per.getMoney());
    		
    		Button button = (Button)this.findViewById(R.id.shut_button);
    		button.setOnClickListener(new View.OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				Intent intent = new Intent();
    				intent.putExtra("name", "hello android ,i am comming");
    				setResult(100, intent);
    				OtherActivity.this.finish();
    			}
    		});
    	}
    	@Override
    	protected void onResume() {
    		Log.i(LOG, "onResume");
    		super.onResume();
    	}
    	@Override
    	protected void onStart() {
    		Log.i(LOG, "onStart");
    		super.onStart();
    	}
    	@Override
    	public boolean onKeyDown(int keyCode, KeyEvent event)
    	{
    		// 返回按键
    		if (keyCode == KeyEvent.KEYCODE_BACK)
    		{
    			new AlertDialog.Builder(this)
    					.setCancelable(false)
    					.setIcon(android.R.drawable.btn_dialog)
    					.setTitle("退出").
    					setMessage("您确认要退出程序吗?")
    					.setPositiveButton("确定", new DialogInterface.OnClickListener()
    					{
    						public void onClick(DialogInterface dialog, int which)
    						{
    							//关闭程序的核心方法
    //							OtherActivity.this.finish();
    							dialog.cancel();
    						}
    					})
    					.setNegativeButton("取消", new DialogInterface.OnClickListener()
    					{
    						
    						public void onClick(DialogInterface dialog, int which)
    						{
    							dialog.cancel();
    						}
    					}).show();
    		}
    		return true;
    	}
    
        
    }
    

    第一个Activity .页面只有一个button.第二个Acivity 有一个TextView 和一个Button.当点击button时,返回到第一个Activity.页面比较简单。

    结果图:


  • 相关阅读:
    lua 逻辑运算 and, or, not
    redis和memcache列出所有key
    Linux protobuf
    CGI,FastCGI,PHP-CGI与PHP-FPM
    lua使用笔记2:Linux 中安装php的lua扩展
    lua使用笔记1:Linux 中安装lua
    Android APK反编译详解
    git 常用命令
    git push 403错误解决方法
    linux(centos)搭建SVN服务器
  • 原文地址:https://www.cnblogs.com/java20130726/p/3218337.html
Copyright © 2020-2023  润新知