android startactivityforresult参数,关于解决页面跳转(startActivityForResult )你还有不知道的!...

1、startactivityForResult 这个页面跳转方法大家都知道,这个方法相对于startactivity的好处在于他可以回收B页面带回来的返回值。大家都知道想要收到回调一定要实现一个方法

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch(requestCode)

{//这个值是我们从A页面跳转时候输入的请求码

switch(resultCode){//这个值是从B页面通过setResult(resultCode)返回的结果码,其中data这个参数便是从B页面返回来给我的值,值是保存在Intent对象中的

}

}

}

但是一般这个回调是在B页面通过Setresult 之后才会执行。但是我之前遇到了。每次通过startactivityForResult()这个方法之后,这个回调就已经执行了。B页面也能正常跳转,但是把B销毁后,并没有执行这个回调了。返回的resultCode也默认是0,这一点令我很奇怪。找了很久才发现原来B页面在MainFest中注册的时候其中lunchMode 用的是singleTask。这种加载模式会导致startactivityForResult() 这个方法失效。此时我们看一下这个方法的源码:

/**

* Launch an activity for which you would like a result when it finished.

* When this activity exits, your

* onActivityResult() method will be called with the given requestCode.

* Using a negative requestCode is the same as calling

* {@link #startActivity} (the activity is not launched as a sub-activity).

*

*

Note that this method should only be used with Intent protocols

* that are defined to return a result. In other protocols (such as

* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), ***

> **[you may

> * not get the result when you expect.

> For example***, if the activity you * are launching uses the

> singleTask launch mode, it will not run in your * task and thus you

> will immediately receive a cancel result.***][1]**

***

*

*

As a special case, if you call startActivityForResult() with a requestCode

* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your

* activity, then your window will not be displayed until a result is

* returned back from the started activity. This is to avoid visible

* flickering when redirecting to another activity.

*

*

This method throws {@link android.content.ActivityNotFoundException}

* if there was no Activity found to run the given Intent.

*

* @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) {

// If this start is requesting a result, we can avoid making

// the activity visible until the result is received. Setting

// this code during onCreate(Bundle savedInstanceState) or onResume() will keep the

// activity hidden during this time, to avoid flickering.

// This can only be done when a result is requested because

// that guarantees we will get information back when the

// activity is finished, no matter what happens to it.

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);

}

}

}

其中加粗那段意思是如果你加载模式设置为singleTask 那么你的任务将不会收到返回值,并且将会立即执行收到一个退出的返回码也就是0。所以以后用的时候一定要注意了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值