startActivityForResult得不到onActivityResult返回值

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

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
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

9 条评论

  1. avatar kozmovip

    <a href="http://www.gibbsorganic.com/nike-soccers-sko-guld-himmelbl%C3%A5-schuhee">nike soccers sko guld himmelbl氓</a> <a href="http://www.malatyapi.com/nike-air-flight-89-r%C3%B8d-and-hvit-foote">nike air flight 89 r酶d and hvit</a> <a href="http://www.calwhitson.com/air-jordan-6-pinnacle-or-soft-skof">air jordan 6 pinnacle or soft</a> <a href="http://www.glamourspausa.com/men-nike-tampa-bay-buccaneers-6-dan-orlovsky-limited-orange-glaze-alternate-nfl-jersey-sale-nflh">men nike tampa bay buccaneers 6 dan orlovsky limited orange glaze alternate nfl jersey sale</a> <a href="http://www.thebeadpro.com/toronto-maple-leafs-third-jersey-2016-nfli">toronto maple leafs third jersey 2016</a> <a href="http://www.mollynurse.com/50-mookie-betts-jersey-nflh">50 mookie betts jersey</a>
    kozmovip http://www.kozmovip.com/

  2. avatar strongtale

    <a href="http://www.michiganev.com/adidas-porsche-design-iii-p5000-m%C3%A4nner-schwarz-gr%C3%BCn-schuheh">adidas porsche design iii p5000 m盲nner schwarz gr眉n</a> <a href="http://www.serdarhan.com/nike-flyknit-air-max-men-grey-black-purple-shoesk">nike flyknit air max men grey black purple</a> <a href="http://www.ankaralimnet.com/michael-michael-kors-jet-set-travel-medium-logo-tote-mkh">michael michael kors jet set travel medium logo tote</a> <a href="http://www.greenwichua.com/nike-lunarglide-6-svart-and-gr%C3%B8nn-obuvi">nike lunarglide 6 svart and gr酶nn</a> <a href="http://www.minicomposer.com/nike-free-4.0-flyknit-femminile-in-esecuzione-scarpa-grigio-scarpek">nike free 4.0 flyknit femminile in esecuzione scarpa grigio</a> <a href="http://www.tabletopsport.com/nike-air-force-1-womens-white-high-shoesi">nike air force 1 womens white high</a>
    strongtale http://www.strongtale.com/

  3. avatar pvktechinfo

    <a href="http://www.momandpopop.com/elite-spencer-long-washington-redskins-road-jersey-61-white-nflb">elite spencer long washington redskins road jersey 61 white</a> <a href="http://www.artlaurey.com/nike-zoom-pegasus-32-womens-grey-paint-skod">nike zoom pegasus 32 womens grey paint</a> <a href="http://www.ochapan.com/burberry-scarf-shop-scarfr">burberry scarf shop</a> <a href="http://www.brooklyngay.com/air-jordan-after-game-herren-orange-sneakera">air jordan after game herren orange</a> <a href="http://www.royphelps.com/billig-nike-free-l%C3%B8ping-sko-kvinners-oransje-shoesb">billig nike free l酶ping sko kvinners oransje</a> <a href="http://www.agneaublanc.com/hugo-boss-mens-tie-tiea">hugo boss mens tie</a>
    pvktechinfo http://www.pvktechinfo.com/

  4. avatar tamisettc

    <a href="http://www.stopbannon.com/cleveland-indians-beanie-hath">cleveland indians beanie</a> <a href="http://www.biteofpr.com/nike-air-yeezy-2-hommes-gris-rose-orange-chaussuresh">nike air yeezy 2 hommes gris rose orange</a> <a href="http://www.askedwhite.com/adidas-originals-gazelle-trainers-wei%C3%9F-schuheh">adidas originals gazelle trainers wei脽</a> <a href="http://www.nigmotion.com/nike-air-2018-flyknit-aq0924-007-cool-grey-black-red-green-shoesk">nike air 2018 flyknit aq0924 007 cool grey black red green</a> <a href="http://www.alalukah.com/kvinders-air-jordan-retro-11-lyser%C3%B8d-skoj">kvinders air jordan retro 11 lyser酶d</a> <a href="http://www.irishpubsbook.com/nike-internationalist-sunset-rosa-schuheh">nike internationalist sunset rosa</a>
    tamisettc http://www.tamisettc.com/

  5. avatar erstube

    <a href="http://www.koreazp.com/nike-air-max-90-hyperfuse-orange-silver-shoesj">nike air max 90 hyperfuse orange silver</a> <a href="http://www.hiltonskyshop.com/asics-onitsuka-tiger-mexico-66-hvid-sort-skoq">asics onitsuka tiger mexico 66 hvid sort</a> <a href="http://www.lavaforchange.com/nike-roshe-run-high-vert-rouge-chaussuresl">nike roshe run high vert rouge</a> <a href="http://www.lifeonfilms.com/air-jordan-retro-28-vert-blanc-chaussuresq">air jordan retro 28 vert blanc</a> <a href="http://www.midlandroofer.com/nike-foamposite-schwarz-suede-on-feet-schuhei">nike foamposite schwarz suede on feet</a> <a href="http://www.pearsonchool.com/adidas-neo-skate-wei%C3%9F-rot-schuheh">adidas neo skate wei脽 rot</a>
    erstube http://www.erstube.com/

  6. avatar rcfitcamp

    <a href="http://www.hinogi.com/adidas-crazylight-boost-r%C3%B8d-oransje-obuvi">adidas crazylight boost r酶d oransje</a> <a href="http://www.prontosignsus.com/nike-lunar-gato-2-naranja-zapatosp">nike lunar gato 2 naranja</a> <a href="http://www.ticahome.com/nike-kobe-11-gul-lilla-skol">nike kobe 11 gul lilla</a> <a href="http://www.prformusic.com/nike-free-tr-femminile-grigio-verde-scarpeh">nike free tr femminile grigio verde</a> <a href="http://www.tatilblogu.com/air-jordan-son-of-mars-rosso-oro-scarpei">air jordan son of mars rosso oro</a> <a href="http://www.danieladirosa.com/nike-air-max-tailwind-7-himmelblau-rosa-schuheh">nike air max tailwind 7 himmelblau rosa</a>
    rcfitcamp http://www.rcfitcamp.com/

  7. avatar realchiyouth

    <a href="http://www.ciaodearborn.com/los-angeles-clippers-mitchell--ness-nba-sandy-off-white-snapback-cap-hath">los angeles clippers mitchell  ness nba sandy off white snapback cap</a> <a href="http://www.ilrione.com/nike-free-3.0-v5-hyper-rosado-trainersce">nike free 3.0 v5 hyper rosado</a> <a href="http://www.bmthtaxis.com/john-varvatos-schwarz-converse-schuhek">john varvatos schwarz converse</a> <a href="http://www.delmotkrt.com/nike-air-force-1-mid-gull-bl%C3%A5-scarpef">nike air force 1 mid gull bl氓</a> <a href="http://www.emutemusic.com/air-jordan-melo-m10-brun-gr%C3%B8n-kurpesb">air jordan melo m10 brun gr酶n</a> <a href="http://www.kinofrost.com/amarillo-converse-chuck-taylor-zapatos-zapatosk">amarillo converse chuck taylor zapatos</a>
    realchiyouth http://www.realchiyouth.com/

  8. avatar kendalfewster

    <a href="http://www.tmzred.com/nike-flyknit-roshe-run-gold-blue-shoesi">nike flyknit roshe run gold blue</a> <a href="http://www.alcovozov.com/nike-lebron-12-low-lilla-skoj">nike lebron 12 low lilla</a> <a href="http://www.ahmztradefx.com/nike-lunar-svart-mennns-obuvi">nike lunar svart mennns</a> <a href="http://www.isampled.com/nike-air-max-90-kvinners-hot-rosa-obuvp">nike air max 90 kvinners hot rosa</a> <a href="http://www.aldervets.com/nike-air-max-90-carved-bl%C3%A5-svart-obuvi">nike air max 90 carved bl氓 svart</a> <a href="http://www.vanltl.com/adidas-zx-900-plata-rosado-zapatosp">adidas zx 900 plata rosado</a>
    [url=http://www.kendalfewster.com/]kendalfewster[/url]

  9. avatar <a>aa</a>

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

欢迎留言




用户登录

sitemap