Activity之间返回结果
来源:互联网 发布:usb端口检测软件 编辑:程序博客网 时间:2024/04/26 03:32
一 、实现:
1、在MainActivity中:
1)onCreate()中:
Intent intent = new Intent(MainActivity.this,OtherActivity.class);startActivityForResult(intent,0); //startActivityForResult(Intent intent,int requestcode)
2)重写onActivityResult():
@overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //处理返回的数据 String name = data.getBundleExtra("data").getString("name"); message.setText(name); }
2、在OtherActivity中:
1)onCreate()中:
Intent intent = this.getIntent();Bundle bundle = new Bundle();bundle.put("name","Jack");intent.putExtra("BundleKey",bundle);setResult(0,intent); //setResult(int resultCode, Intent intent);
二 、过程:
1)首先,来看startActivityForResult(intent,0);
进入FragmentActivity.java:
/** * Modifies the standard behavior to allow results to be delivered to fragments. * This imposes a restriction that requestCode be <= 0xffff. */ @Override public void startActivityForResult(Intent intent, int requestCode) { if (requestCode != -1 && (requestCode&0xffff0000) != 0) { throw new IllegalArgumentException("Can only use lower 16 bits for requestCode"); } super.startActivityForResult(intent, requestCode); }
判断requestCode是否小于16 bits,
如果requestCode <= 16bits,再进入Activity.java:
/** * Same as calling {@link #startActivityForResult(Intent, int, Bundle)} * with no options. * * @param intent The intent to start. * @param requestCode If >= 0, this code will be returned in * onActivityResult() when the activity exits. * * @throws android.content.ActivityNotFoundException * * @see #startActivity */ public void startActivityForResult(Intent intent, int requestCode) { startActivityForResult(intent, requestCode, null); } /** * @param intent The intent to start. * @param requestCode If >= 0, this code will be returned in * onActivityResult() when the activity exits. * @param options Additional options for how the Activity should be started. * See {@link android.content.Context#startActivity(Intent, Bundle) * Context.startActivity(Intent, Bundle)} for more details. * * @throws android.content.ActivityNotFoundException * * @see #startActivity */ public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) { if (mParent == null) { Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, options); if (ar != null) { mMainThread.sendActivityResult( mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); } if (requestCode >= 0) { mStartedActivity = true; } cancelInputsAndStartExitTransition(options); // TODO Consider clearing/flushing other event sources and events for child windows. } else { if (options != null) { mParent.startActivityFromChild(this, intent, requestCode, options); } else { // Note we want to go through this method for compatibility with // existing applications that may have overridden it. mParent.startActivityFromChild(this, intent, requestCode); } } }
判断requestCode是否大于0,
If requestCode >= 0, this code will be returned in onActivityResult() when the activity exits.
2)然后,来看setResult(0,intent);
进入Activity.java:
/** * Call this to set the result that your activity will return to its * caller. * * <p>As of {@link android.os.Build.VERSION_CODES#GINGERBREAD}, the Intent * you supply here can have {@link Intent#FLAG_GRANT_READ_URI_PERMISSION * Intent.FLAG_GRANT_READ_URI_PERMISSION} and/or {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION * Intent.FLAG_GRANT_WRITE_URI_PERMISSION} set. This will grant the * Activity receiving the result access to the specific URIs in the Intent. * Access will remain until the Activity has finished (it will remain across the hosting * process being killed and other temporary destruction) and will be added * to any existing set of URI permissions it already holds. * * @param resultCode The result code to propagate back to the originating * activity, often RESULT_CANCELED or RESULT_OK * @param data The data to propagate back to the originating activity. * * @see #RESULT_CANCELED * @see #RESULT_OK * @see #RESULT_FIRST_USER * @see #setResult(int) */ public final void setResult(int resultCode, Intent data) { synchronized (this) { mResultCode = resultCode; mResultData = data; } }
把resultCode,data返回给MainActivity的onActivityResult(int requestCode, int resultCode, Intent data)
3)@override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //处理返回的数据 }
明白其中的原理,学起来会很快,加油!
0 0
- Activity之间返回结果
- 从Activity返回结果
- 调用Activity返回结果
- Activity处理返回结果
- android activity之间传递参数,获取上一个activity的返回结果
- 使用Bundle在Activity之间交换数据、调用另一个Activity并返回结果
- 启动Activity并返回结果
- Activity调用另一Activity并返回结果
- Android 启动另外activity并返回结果
- Android Activity返回结果startActivityForResult函数
- startActivityForResult启动另一个Activity并返回结果
- 启动其他Activity并返回结果
- 调用一个Activity并返回结果
- 从Activity返回结果出现异常
- 打开activity,关闭后返回会结果
- Android Activity返回结果startActivityForResult函数
- 启动其他的Activity并返回结果
- Android 启动其他Activity并返回结果
- HP实训软件测试第一阶段发现的问题
- 代码
- Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
- C#学习之多线程开发技术(十一)
- MYISAM表批量压缩
- Activity之间返回结果
- php date 处理
- Log4j1X迁移至Log4j2(基于SLF4j)
- 关系演算
- iOS编程------动画
- 软件测试行业浅析
- 装饰模式
- 1023. 组个最小数 (20)
- POJ 2533 Longest Ordered Subsequence (LIS)