startActivityForResult得不到onActivityResult返回值

2015年12月17日 Android 17条评论 阅读4040次

startActivityForResult得不到onActivityResult返回值

警惕android:launchMode="singleInstance"导致startActivityForResult得不到onActivityResult返回值。

原始标题想用这个的,但因太长了,就简略了一下。

问题的现象很简单。 ActivityA中一个按钮,点击触发的事件:

startActivityForResult(new Intent(this, ActivityB.class));

在ActivityB中,设置返回的结果:

setResult(RESULT_OK, intent);
ActivityB.this.finish();

回到ActivityA:回调方法

onActivityResult(int requestCode, int resultCode, Intent data)

期待是resultCode为RESULT_OK,并从data中得到想要的数据。

但遗憾的是resultCode并不是RESULT_OK,而是RESULT_CANCELED。

好了,发生的问题现象基本就是这样子。 原因在开头已经透漏了。就是activity的launchMode被设置为单例模式了。 解决方法: 只要把AndroidManifest.xml中的android:launchMode="singleInstance"删除即可。

android.app.Activity类中的如下方法 public void startActivityForResult(Intent intent, int requestCode, Bundle options) 有下面的注释说明

Note that this method should only be used with Intent protocols that are defined to return a result. In other protocols (such as Intent.ACTION_MAIN or 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.


重新温习Activity的启动模式

以下转载自Hibraincol的cnblogs,多谢---------

  • standard
  • singleTop
  • singleTask
  • singleInstance

这4中模式又分两类,standard和signleTop属于一类, singleTask和signleInstance属于另一类。

  • standard和singleTop属性的 Activity 的实例可以属于任何任务(Task),并且可以位于Activity堆栈的任何位置。比较典型的一种情况是,一个任务的代码执行startActivity(),如果传递的 Intent 对象没有包含 FLAG_ACTIVITY_NEW_TASK 属性, 指定的 Activity 将被该任务调用,从而装入该任务的Activity 堆栈中。
  • standard和singleTop的区别在于:standard模式的Activity在被调用时会创建一个新的实例,所有实例处理同一个Intent对象; 但对于singleTop模式的Activity,如果被调用的任务已经有一个这样的Activity 在堆栈的顶端,那么不会有新的实例创建, 任务会使用当前顶端的Activity实例来处理Intent对象,换句话说,如果被调用的任务包含一个不在堆栈顶端的 singleTop Activity, 或者堆栈顶端为 singleTop 的Activity的任务不是当前被调用的任务,那么,仍然会有一个新的Activity对象被创建。

singleTask 和 singleInstance模式的Activity 仅可用于启动任务的情况,这种模式的Activity总是处在Activity堆栈的最底端,并且一个任务中只能被实例化一次。

两者的区别在于:对于 singleInstance模式的Activity, 任务的Activity堆栈中如果有这样的Activity,那它将是堆栈中的唯一的 Activity, 当前任务收到的 Intent 都由它处理, 由它开启的其他 Activity 将在其他任务中被启动; 对于 SingleTask模式的Activity,它在堆栈底端,其上方可以有其他Activity被创建, 但是,如果发给该Activity的Intent对象到来时该Activity不在堆栈顶端,那么该Intent对象将被丢弃,但是界面还是会切换到当前的Activity。

--------以上为转载内容,转载结束的分割线---------------


另外,还要注意调用startActivityForResult时的requestCode应该要传递一个非负整数。不多解释,直接附上源码。

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

startactivityforresult-cannot-get-reponsedata-launchMode

分享本文至:

WRITTEN BY

avatar
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

17 条评论

  1. <a href="http://www.apconcierge.com/giannis-antetokounmpo-t-shirt-jersey-nflz">giannis antetokounmpo t shirt jersey</a> <a href="http://www.freonsystem.com/hombres-nike-roshe-run-slip-rosado-naranja-kurpesa">hombres nike roshe run slip rosado naranja</a> <a href="http://www.jupitertaxx.com/roshe-run-schwarz-and-wei%C3%9F-for-verkauf-butyf">roshe run schwarz and wei脽 for verkauf</a> <a href="http://www.khyenchen.com/nike-mens-warren-sapp-limited-black-jersey-tampa-bay-buccaneers-nfl-99-2016-salute-to-service-nflw">nike mens warren sapp limited black jersey tampa bay buccaneers nfl 99 2016 salute to service</a> <a href="http://www.latinphonics.com/asics-gel-quantum-360-blanco-rosado-butyd">asics gel quantum 360 blanco rosado</a> <a href="http://www.liaqui.com/air-max-90-hyperfuse-verde-arancia-kurpesc">air max 90 hyperfuse verde arancia</a>
    alverish http://www.alverish.com/

  2. <a href="http://www.fencespacer.com/nike-air-max-2014-kvinders-sort-gr%C3%A5-chaussuresg">nike air max 2014 kvinders sort gr氓</a> <a href="http://www.garabebbe.com/nike-air-presto-ultra-flyknit-rose-noir-obuvc">nike air presto ultra flyknit rose noir</a> <a href="http://www.amordeblogger.com/air-jordan-imminent-naranja-kurpesb">air jordan imminent naranja</a> <a href="http://www.astrokasa.com/adidas-eqt-bambini-scarpe-rosso-grigio-kurpese">adidas eqt bambini scarpe rosso grigio</a> <a href="http://www.bnlracing.com/nike-zoom-pegasus-31-viola-fiore-kurpesd">nike zoom pegasus 31 viola fiore</a> <a href="http://www.casbloom.com/nike-flyknit-roshe-run-tutti-nero-scarped">nike flyknit roshe run tutti nero</a>
    fufengnc http://www.fufengnc.com/

  3. <a href="http://www.teamdalarna.com/air-jordan-retro-2-purple-blue-shoesh">air jordan retro 2 purple blue</a> <a href="http://www.syairshamyoun.com/mujeres-nike-air-max-thea-p%C3%BArpura-naranja-zapatosp">mujeres nike air max thea p煤rpura naranja</a> <a href="http://www.fizalisfresh.com/nike-blazer-rojo-naranja-zapatosk">nike blazer rojo naranja</a> <a href="http://www.fsscas.com/north-face-m-gotham-jacket-northfaceh">north face m gotham jacket</a> <a href="http://www.imagimp.com/nike-hypershift-grau-himmelblau-schuhej">nike hypershift grau himmelblau</a> <a href="http://www.burmamaritime.com/ua-micro-g-torch-alle-bl%C3%A5-skoh">ua micro g torch alle bl氓</a>
    voydnet http://www.voydnet.com/

  4. <a href="http://www.helpforaspen.com/kvinders-nike-air-max-90-r%C3%B8d-gr%C3%A5-schuhea">kvinders nike air max 90 r酶d gr氓</a> <a href="http://www.mcmartial.com/yeezy-350-boost-blu-note-scarpec">yeezy 350 boost blu note</a> <a href="http://www.puurderleven.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a> <a href="http://www.ipartybooths.com/air-max-1-negro-cuero-kurpesb">air max 1 negro cuero</a> <a href="http://www.senegalreview.com/nike-air-max-womens-zapatost">nike air max womens</a> <a href="http://www.poshandpearls.com/girls-high-converse-shoesz">girls high converse</a>
    golfbodyshop http://www.golfbodyshop.com/

  5. <a href="http://www.masterelief.com/nike-air-max-95-engineerojo-mesh-negro-naranja-kurpesa">nike air max 95 engineerojo mesh negro naranja</a> <a href="http://www.nwlcenter.com/kobe-9-nero-and-bianca-camo-butyg">kobe 9 nero and bianca camo</a> <a href="http://www.piezoinjector.com/nike-youth-tj-jones-limited-blue-alternate-jersey-detroit-lions-nfl-13-vapor-untouchable-nflw">nike youth tj jones limited blue alternate jersey detroit lions nfl 13 vapor untouchable</a> <a href="http://www.printonlipro.com/nike-zoom-pegasus-34-gull-hvit-butyd">nike zoom pegasus 34 gull hvit</a> <a href="http://www.biagricola.com/air-jordan-melo-m10-wei%C3%9F-blau-kurpesc">air jordan melo m10 wei脽 blau</a> <a href="http://www.dennygorman.com/ralph-lauren-mens-burgundy-2009-cotton-hoodie-polos">ralph lauren mens burgundy 2009 cotton hoodie</a>
    dankpixel http://www.dankpixel.com/

  6. <a href="http://www.daddeaze.com/adidas-zx-flux-womens-black-quartz-skof">adidas zx flux womens black quartz</a> <a href="http://www.debracousins.com/flyknit-lunar-3-toronto-raptor-skob">flyknit lunar 3 toronto raptor</a> <a href="http://www.yankaripark.com/atlanta-braves-bucket-hat-uky-hatd">atlanta braves bucket hat uky</a> <a href="http://www.yicailottery.com/nike-shoes-air-max-2015-price-in-philippines-runninge">nike shoes air max 2015 price in philippines</a> <a href="http://www.calebcraw.com/nike-zoom-pegasus-33-dam%C3%A4nner-lila-himmelblau-schuhea">nike zoom pegasus 33 dam盲nner lila himmelblau</a> <a href="http://www.liquidputter.com/air-jordan-imminent-schwarz-orange-scarpeb">air jordan imminent schwarz orange</a>
    cialislaw http://www.cialislaw.com/

  7. avatar mylinkstv

    <a href="http://www.bhcsboosters.com/nike-zoom-vomero-womens-purple-silver-runningf">nike zoom vomero womens purple silver</a> <a href="http://www.denisgoulart.com/zdeno-chara-33-boston-bruins-reebok-premier-alternate-jersey-small-nflv">zdeno chara 33 boston bruins reebok premier alternate jersey small</a> <a href="http://www.donggeomdo.com/nike-air-force-1-flyknit-low-mens-green-orange-trainersa">nike air force 1 flyknit low mens green orange</a> <a href="http://www.ozoessaouira.com/nike-lunar-r%C3%B8d-hvid-scarpec">nike lunar r酶d hvid</a> <a href="http://www.annomaplexel.com/nike-free-run-5.0-v4-azul-negro-kurpesc">nike free run 5.0 v4 azul negro</a> <a href="http://www.bringribben.com/ray-ban-cats-3000-diamond-hard-yellow-sunglassesg">ray ban cats 3000 diamond hard yellow</a>
    mylinkstv http://www.mylinkstv.com/

  8. avatar rahsaskatoon

    <a href="http://www.bealfrance.com/adidas-nmd-runner-sko-kvinners-chaussuresf">adidas nmd runner sko kvinners</a> <a href="http://www.brooklyngay.com/under-armour-clutchfit-drive-tutti-star-codes-butya">under armour clutchfit drive tutti star codes</a> <a href="http://www.demointegrate.com/yeezy-boost-950-gelb-schuheg">yeezy boost 950 gelb</a> <a href="http://www.diggingwp.com/ralph-lauren-shorts-negro-rosado-poloz">ralph lauren shorts negro rosado</a> <a href="http://www.frankiesvoice.com/asics-gel-quantum-360-mennns-svart-light-chaussuresa">asics gel quantum 360 mennns svart light</a> <a href="http://www.hunnyblu.com/air-jordan-17-svart-s%C3%B8lv-imaging-schuhee">air jordan 17 svart s酶lv imaging</a>
    rahsaskatoon http://www.rahsaskatoon.com/

  9. avatar colixguides

    <a href="http://www.mcdevittmedia.com/nike-dunk-sky-high-metallic-rosa-butyg">nike dunk sky high metallic rosa</a> <a href="http://www.thecancerinfo.com/mitchell-and-ness-athletics-35-rickey-henderson-grey-stitched-mlb-jersey-nflw">mitchell and ness athletics 35 rickey henderson grey stitched mlb jersey</a> <a href="http://www.tibetwoodcut.com/maschio-nike-air-max-95-grigio-verde-butye">maschio nike air max 95 grigio verde</a> <a href="http://www.viagralaw.com/adidas-zx-750-wv-noir-jaune-kurpesd">adidas zx 750 wv noir jaune</a> <a href="http://www.agriind.com/nero-polo-sweater-polos">nero polo sweater</a> <a href="http://www.cossidae.com/nike-free-5.0-kvinders-uk-lyser%C3%B8d-butye">nike free 5.0 kvinders uk lyser酶d</a>
    colixguides http://www.colixguides.com/

  10. avatar dinerwaresb

    <a href="http://www.miendiation.com/new-balance-990-purple-blue-runninge">new balance 990 purple blue</a> <a href="http://www.powfarm.com/adidas-pr%C3%B8dator-uk-salg-schuhea">adidas pr酶dator uk salg</a> <a href="http://www.nongmobacon.com/adidas-battle-pack-projoator-mercadolibre-scarpeb">adidas battle pack projoator mercadolibre</a> <a href="http://www.rentalloka.com/nike-air-max-95-green-spain-shoeso">nike air max 95 green spain</a> <a href="http://www.casamentosweb.com/nike-flyknit-racer-lagoon-blau-label-kurpesb">nike flyknit racer lagoon blau label</a> <a href="http://www.perheopas.com/air-jordan-phase-23-zapatost">air jordan phase 23</a>
    dinerwaresb http://www.dinerwaresb.com/

  11. avatar atolyekarinca

    <a href="http://www.acforlessllc.com/tom-brady-michigan-jersey-for-sale-nflu">tom brady michigan jersey for sale</a> <a href="http://www.thehodostore.com/adidas-boost-black-and-gold-skou">adidas boost black and gold</a> <a href="http://www.gandoff.com/air-jordan-6-womens-red-runninge">air jordan 6 womens red</a> <a href="http://www.langbenlang.com/nike-air-max-tricky-quilted-pack-zapatosu">nike air max tricky quilted pack</a> <a href="http://www.glennloovens.com/air-jordan-13-university-r%C3%B8d-alert-chaussuresa">air jordan 13 university r酶d alert</a> <a href="http://www.kamilotomotiv.com/nike-rams-18-cooper-kupp-white-youth-stitched-elite-jersey-nflc">nike rams 18 cooper kupp white youth stitched elite jersey</a>
    atolyekarinca http://www.atolyekarinca.com/

  12. avatar naomisbooks

    <a href="http://www.blockboymusic.com/billig-nike-free-3.0-v2-herre-gr%C3%A5-sneakere">billig nike free 3.0 v2 herre gr氓</a> <a href="http://www.aquaboutic.com/jeremiah-sirles-78-jersey-xl-nfld">jeremiah sirles 78 jersey xl</a> <a href="http://www.plusjulesmumm.com/air-max-reflective-adidas-shoesc">air max reflective adidas</a> <a href="http://www.tomscave.com/air-jordan-future-red-michael-jordan-skof">air jordan future red michael jordan</a> <a href="http://www.casagranderox.com/air-jordan-retro-14-wolf-grey-sport-blue-van-sportse">air jordan retro 14 wolf grey sport blue van</a> <a href="http://www.clumsford.com/nike-free-3.0-v4-black-cheetah-runningd">nike free 3.0 v4 black cheetah</a>
    naomisbooks http://www.naomisbooks.com/

  13. avatar minneimaging

    <a href="http://www.checkmyvape.com/adidas-pr%C3%B8dator-mania-blades-butyf">adidas pr酶dator mania blades</a> <a href="http://www.hagertools.com/kyrie-3-university-rojo-deer-obuvf">kyrie 3 university rojo deer</a> <a href="http://www.kangenkitchen.com/nike-roshe-run-blanco-negro-hyper-punch-obuvd">nike roshe run blanco negro hyper punch</a> <a href="http://www.oricastg.com/nike-internationalist-negro-dark-mica-verde-kurpesg">nike internationalist negro dark mica verde</a> <a href="http://www.themenfinder.com/vibram-fivefingers-classic-lila-schwarz-obuvb">vibram fivefingers classic lila schwarz</a> <a href="http://www.turkmendili.com/nike-air-huarache-dam%C3%A4nner-rot-himmelblau-butya">nike air huarache dam盲nner rot himmelblau</a>
    minneimaging http://www.minneimaging.com/

  14. avatar nicakhtar

    <a href="http://www.diveintovideo.com/red-wings-14-brendan-shanahan-red-ccm-throwback-stitched-nhl-jersey-nfls">red wings 14 brendan shanahan red ccm throwback stitched nhl jersey</a> <a href="http://www.myhybridshop.com/herren-ralph-lauren-shirt-gr%C3%BCn-polot">herren ralph lauren shirt gr眉n</a> <a href="http://www.nrthofhere.com/calgary-flames-camo-jersey-nflz">calgary flames camo jersey</a> <a href="http://www.torilsdesign.com/maschio-nike-roshe-yeezy-boost-oro-nero-kurpesc">maschio nike roshe yeezy boost oro nero</a> <a href="http://www.bishoplitman.com/mens-nike-buffalo-bills-32-o-j-simpson-game-white-nfl-jersey-nflz">mens nike buffalo bills 32 o j simpson game white nfl jersey</a> <a href="http://www.ksjfifgmif.com/jordan-retro-13-gr%C3%A5-rosa-obuvd">jordan retro 13 gr氓 rosa</a>
    nicakhtar http://www.nicakhtar.com/

  15. avatar myflipsstore

    <a href="http://www.akncmusic.com/polo-ralph-lauren-fleece-contrast-hoodie-polot">polo ralph lauren fleece contrast hoodie</a> <a href="http://www.dystopianflix.com/nike-super-bowl-lii-youth-james-develin-limited-olivecamo-jersey-new-england-patriots-nfl-46-2017-salute-to-service-nflx">nike super bowl lii youth james develin limited olivecamo jersey new england patriots nfl 46 2017 salute to service</a> <a href="http://www.getunladylike.com/air-jordan-1-silber-medal-awards-kurpesc">air jordan 1 silber medal awards</a> <a href="http://www.jesselyon.com/nike-ash-kiko-alonso-one-color-nfl-47-miami-dolphins-pullover-hoodie-nflx">nike ash kiko alonso one color nfl 47 miami dolphins pullover hoodie</a> <a href="http://www.kabukcuoglu.com/nike-magista-cleats-gr%C3%B8n-hvid-obuvd">nike magista cleats gr酶n hvid</a> <a href="http://www.karbasioun.com/nike-air-max-tailwind-6-negro-rojo-scarpec">nike air max tailwind 6 negro rojo</a>
    myflipsstore http://www.myflipsstore.com/

  16. avatar oldoyopark

    <a href="http://www.ntygadget.com/nike-hyperrev-2015-black-and-gold-weapons-sneakere">nike hyperrev 2015 black and gold weapons</a> <a href="http://www.purchasetulsa.com/lebron-14-wei%C3%9F-and-gold-roshe-kurpesa">lebron 14 wei脽 and gold roshe</a> <a href="http://www.pennywave.com/sb-stefan-janoski-max-blue-volt-sneakerb">sb stefan janoski max blue volt</a> <a href="http://www.scoptix.com/air-max-2016-london-shoesg">air max 2016 london</a> <a href="http://www.domodrzwi.com/trucker-hat-quiksilver-hats">trucker hat quiksilver</a> <a href="http://www.magneticduo.com/adidas-rose-grey-orange-sneakerf">adidas rose grey orange</a>
    [url=http://www.oldoyopark.com/]oldoyopark[/url]

  17. avatar <a>aa</a>

    oracle数据库查看用户锁表及对表解锁的sql语句

欢迎留言




用户登录

sitemap