首页 > 代码库 > startActivityForResult的用法和demo
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(); } }); } }
- ActivityForResultDemo.zip (47.9 KB)下载次数: 142
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。