startActivityForResult的用法和demo

有时候我们需要把A activity提交数据给B  activity处理,然后把结果返回给A

 

这种方式在很多种情况需要用到,比如我应用的程序需要有拍照上传的功能。

 

一种解决方案是  我的应用程序 〉调用系统相机〉拍照〉关闭系统相机〉返回拍照后的照片到我的应用程序

 

 

 

 

启动一个ForResult的意图:

Intent intent = new Intent(MainAcitvity.this,
      RequestActivity.class);
    //发送意图标示为REQUSET=1
    startActivityForResult(intent, REQUSET);

 

 

B Activity处理数据:

 

Intent intent=new Intent();
    intent.putExtra(KEY_USER_ID, et01.getText().toString());
    intent.putExtra(KEY_USER_PASSWORD, et02.getText().toString());
setResult(RESULT_OK, intent);
 finish();

 

 

那么startActivityForResult如何使用呢:

 

package com.cn.activityresult.demo;  
  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
import android.widget.Toast;  
  
public class MainAcitvity extends Activity {  
  
    private Button btn01;  
    private TextView tv01;  
    public static final int REQUSET = 1;  
  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        // TODO Auto-generated method stub  
        super.onActivityResult(requestCode, resultCode, data);  
        //requestCode标示请求的标示   resultCode表示有数据  
        if (requestCode == MainAcitvity.REQUSET && resultCode == RESULT_OK) {  
            String str = "账号"  
                    + data.getStringExtra(RequestActivity.KEY_USER_ID) + "\n"  
                    + "密码"  
                    + data.getStringExtra(RequestActivity.KEY_USER_PASSWORD);  
            tv01.setText(str);  
        }  
        Toast.makeText(  
                this,  
                "requestCode=" + requestCode + ":" + "resultCode=" + resultCode,  
                Toast.LENGTH_LONG).show();  
    }  
  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        btn01 = (Button) findViewById(R.id.btn01);  
        tv01 = (TextView) findViewById(R.id.tv01);  
        btn01.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                Intent intent = new Intent(MainAcitvity.this,  
                        RequestActivity.class);  
                //发送意图标示为REQUSET=1  
                startActivityForResult(intent, REQUSET);  
            }  
        });  
  
    }  
}  

 

 

 

import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
  
public class RequestActivity extends Activity {  
      
    private Button btn01;  
    private EditText et01;  
    private EditText et02;  
    public static final String KEY_USER_ID="KEY_USER_ID";  
    public static final String KEY_USER_PASSWORD="KEY_USER_PASSWORD";  
      
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.request_activity);  
        btn01=(Button)findViewById(R.id.btn01);  
        et01=(EditText)findViewById(R.id.et01);  
        et02=(EditText)findViewById(R.id.et02);  
          
          
        btn01.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                Intent intent=new Intent();  
                intent.putExtra(KEY_USER_ID, et01.getText().toString());  
                intent.putExtra(KEY_USER_PASSWORD, et02.getText().toString());  
                setResult(RESULT_OK, intent);  
                finish();  
            }  
        });           
    }  
}  
 

 

posted @ 2014-07-20 20:32  n1rAy  阅读(1507)  评论(0编辑  收藏  举报